Behavior of apply()
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.