I want to split a matrix, where both 'u' and 'w' are results of possible ways. However, whenever 'n' changes, the argument passed to mapply() has to change. Is there a way to pass elements of a list as multiple arguments? m=10 n=2 k=3 set.seed(0) x=replicate(n,rnorm(m)) f=sample(1:k, size=m, replace=T) u=split(as.data.frame(x),f) v=lapply( 1:dim(x)[[2]] , function(i) { split(x[,i],f) } ) w=mapply( function(x,y) { cbind(x,y) } , v[[1]], v[[2]] )
David Winsemius
2009-Dec-09 05:05 UTC
[R] Can elements of a list be passed as multiple arguments?
On Dec 8, 2009, at 11:37 PM, Peng Yu wrote:> I want to split a matrix, where both 'u' and 'w' are results of > possible ways. However, whenever 'n' changes, the argument passed to > mapply() has to change. Is there a way to pass elements of a list as > multiple arguments?You need to explain what you want in more detail. In your example mapply did exactly what you told it to. No errors. Three matrices. What were you expecting when you gave it three lists in each argument?> > m=10 > n=2 > k=3 > > set.seed(0) > x=replicate(n,rnorm(m)) > f=sample(1:k, size=m, replace=T) > > u=split(as.data.frame(x),f) > > v=lapply( > 1:dim(x)[[2]] > , function(i) { > split(x[,i],f) > } > ) > > w=mapply( > function(x,y) { > cbind(x,y) > } > , v[[1]], v[[2]] > )-- David Winsemius, MD Heritage Laboratories West Hartford, CT