Suppose I have the following matrix> class(cov_50)[1] "matrix"> cov_50[,1] [,2] [1,] 0.3201992 2.308084 [2,] 6.7312928 5.719641 I then use the following function via apply and get the desired output, a list signif <- function(x) which(abs(x) > 1.96) apply(cov_50, 1, signif)> apply(cov_50, 1, signif)[[1]] [1] 2 [[2]] [1] 1 2 However, I can't see why the following occurs> class(cov_25)[1] "matrix"> cov_25[,1] [,2] [1,] 12.116106 14.81248 [2,] 5.492176 4.73138> apply(cov_25, 1, signif)[,1] [,2] [1,] 1 1 [2,] 2 2 So, using the same function (signif) on a different object of the same class yields a matrix and *not* a list as desired. I can't see any reason for the different output.> sessionInfo()R version 2.12.2 (2011-02-25) Platform: x86_64-pc-mingw32/x64 (64-bit) locale: [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] quantreg_4.57 SparseM_0.86 [[alternative HTML version deleted]]
On Dec 8, 2011, at 3:30 PM, Doran, Harold wrote:> Suppose I have the following matrix > >> class(cov_50) > [1] "matrix" >> cov_50 > [,1] [,2] > [1,] 0.3201992 2.308084 > [2,] 6.7312928 5.719641 > > I then use the following function via apply and get the desired output, a list > > signif <- function(x) which(abs(x) > 1.96) > apply(cov_50, 1, signif) > >> apply(cov_50, 1, signif) > [[1]] > [1] 2 > > [[2]] > [1] 1 2 > > However, I can't see why the following occurs > >> class(cov_25) > [1] "matrix" >> cov_25 > [,1] [,2] > [1,] 12.116106 14.81248 > [2,] 5.492176 4.73138 > >> apply(cov_25, 1, signif) > [,1] [,2] > [1,] 1 1 > [2,] 2 2 > > So, using the same function (signif) on a different object of the same class yields a matrix and *not* a list as desired. > > I can't see any reason for the different output.Harold, Per the Value section of ?apply: If the calls to FUN return vectors of different lengths, apply returns a list of length prod(dim(X)[MARGIN]) with dim set to MARGIN if this has length greater than one. In your first example, the first row only has one value that is returned by signif() and then second row has two. So a list is returned. In your second example, both rows have two values that are returned by signif(), thus a matrix can be returned. The key is not that the two objects are both matrices, but that there are differing lengths in the returned values from the rows in each. HTH, Marc Schwartz
It's because your first example produces answers of varying length so there's no natural way to coerce it to a matrix. Your second has a consistent length so the result can be made into a matrix. Michael On Thu, Dec 8, 2011 at 4:30 PM, Doran, Harold <HDoran at air.org> wrote:> Suppose I have the following matrix > >> class(cov_50) > [1] "matrix" >> cov_50 > ? ? ? ? ?[,1] ? ? [,2] > [1,] 0.3201992 2.308084 > [2,] 6.7312928 5.719641 > > I then use the following function via apply and get the desired output, a list > > signif <- function(x) which(abs(x) > 1.96) > apply(cov_50, 1, signif) > >> apply(cov_50, 1, signif) > [[1]] > [1] 2 > > [[2]] > [1] 1 2 > > However, I can't see why the following occurs > >> class(cov_25) > [1] "matrix" >> cov_25 > ? ? ? ? ?[,1] ? ? [,2] > [1,] 12.116106 14.81248 > [2,] ?5.492176 ?4.73138 > >> ?apply(cov_25, 1, signif) > ? ? [,1] [,2] > [1,] ? ?1 ? ?1 > [2,] ? ?2 ? ?2 > > So, using the same function (signif) on a different object of the same class yields a matrix and *not* a list as desired. > > I can't see any reason for the different output. > >> sessionInfo() > R version 2.12.2 (2011-02-25) > Platform: x86_64-pc-mingw32/x64 (64-bit) > > locale: > [1] LC_COLLATE=English_United States.1252 ?LC_CTYPE=English_United States.1252 > [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C > [5] LC_TIME=English_United States.1252 > > attached base packages: > [1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base > > other attached packages: > [1] quantreg_4.57 SparseM_0.86 > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.