[R] converting apply output
Andrew Beckerman
a.beckerman at sheffield.ac.uk
Wed Jun 23 12:53:10 CEST 2004
Hi -
platform powerpc-apple-darwin6.8
status
major 1
minor 9.0
year 2004
I am trying to deal with the output of apply(). As indicated, when
each call to 'FUN' returns a vector of length 'n', then 'apply'
returns an array of dimension 'c(n, dim(X)[MARGIN])'. However, I would
like this to be a list in the same format as is produced when 'FUN'
return vectors of different lengths ('apply' returns a list of length
'dim(X)[MARGIN]').
e.g.
tt1<-c(0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ,0 ,0 ,0 ,0 ,0 ,0
,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,0
,1 ,0 ,0 ,0 ,0 ,0 ,1 ,0 ,0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 1, 0)
m1<-matrix(tt1,10,10)
out<-apply(m1,2,function(x) which(x==1))
produces an array,
> out
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 8 9 7 8 6 7 6 6 6 7
[2,] 9 10 8 9 8 8 10 10 7 9
but I would like out as a list of 10 elements with two elements in
each, e.g.
[[1]]
[1] 8 9
[[2]]
[1] 9 10
etc.
I have tried apply(out,2,function(x) list(x))), but the subsrcripting
is not equal to the pattern when FUN returns a vectors of different
length. Any help would be appreciated.
Cheers
andrew
More information about the R-help
mailing list