[R] converting apply output
Andrew Beckerman
a.beckerman at sheffield.ac.uk
Wed Jun 23 19:48:20 CEST 2004
Thanks eric... I figured this routine out as well.
cvt<-function(dat){
x<-as.list(rep(0,dim(dat)[2]))
for(i in 1:dim(dat)[2]){
x[[i]]<-dat[,i]
x}}
# get ragged array of 1's
dat<-apply(mat,2,function(x) which(x==1))
# deal with this using cvt to creat list
if(is.null(dim(dat))) dat2<-dat else dat2<-cvt(dat)
On 23 Jun 2004, at 13:13, Eric Lecoutre wrote:
>
> 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
>
>
------------------------------------------------------------------------
---------
Dr. Andrew Beckerman
Department of Animal and Plant Sciences, University of Sheffield,
Alfred Denny Building, Western Bank, Sheffield S10 2TN, UK
ph +44 (0)114 222 0026; fx +44 (0)114 222 0002
http://www.shef.ac.uk/beckslab
------------------------------------------------------------------------
----------
More information about the R-help
mailing list