[R] converting apply output

Eric Lecoutre lecoutre at stat.ucl.ac.be
Wed Jun 23 14:13:45 CEST 2004


Hi,

Why not try with the data.frame structure, wich internally yet consists in 
a list:

 > lapply(as.data.frame(m1),function(x) which(x==1))
$V1
[1] 8 9
$V2
[1]  9 10
[...]

Eric

At 12:53 23/06/2004, Andrew Beckerman wrote:
>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
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Eric Lecoutre
UCL /  Institut de Statistique
Voie du Roman Pays, 20
1348 Louvain-la-Neuve
Belgium

tel: (+32)(0)10473050
lecoutre at stat.ucl.ac.be
http://www.stat.ucl.ac.be/ISpersonnel/lecoutre

If the statistics are boring, then you've got the wrong numbers. -Edward 
Tufte




More information about the R-help mailing list