[R] Using unsplit - unsplit does not seem to reverse the effect of split

Peter Dalgaard p.dalgaard at biostat.ku.dk
Tue Sep 27 19:49:33 CEST 2005


Søren Højsgaard <Soren.Hojsgaard at agrsci.dk> writes:

> In data OME in MASS I would like to extract the first 5 observations per subject (=ID). So I do
>  
> library(MASS)
> OMEsub <- split(OME, OME$ID)
> OMEsub <- lapply(OMEsub,function(x)x[1:5,])
> unsplit(OMEsub, OME$ID)
> 
> - which results in 
>  
> [[1]]
> [1] 1 1 1 1 1
> [[2]]
> [1] 30 30 30 30 30
> [[3]]
> [1] low low low low low
> Levels: N/A high low
> [[4]]
> [1] 35 35 40 40 45
> [[5]]
> [1] coherent   incoherent coherent   incoherent coherent  
> Levels: coherent incoherent
> [[6]]
> [1] 1 4 0 1 2
> 
> ............
>  
> [[1094]]
> [1] 4 5 5 5 2
> [[1095]]
> [1] 100 100 100 100 100
> [[1096]]
> [1] 18 18 18 18 18
> [[1097]]
> [1] N/A N/A N/A N/A N/A
> Levels: N/A high low
> There were 50 or more warnings (use warnings() to see the first 50)
> 
> warnings()
> Warning messages:
> 1: number of items to replace is not a multiple of replacement length
> 2: number of items to replace is not a multiple of replacement length
> 3: number of items to replace is not a multiple of replacement length
> ....
>  
> According to documentation unsplit is the reverse of split, but I must be missing a point somewhere... Can anyone help? Thanks in advance. Søren

It only works if the first argument is or could have resulted from a
split on the second argument. That is clearly not the case when you
are creating subvectors. 

I have on occasion wanted an unsplit that worked without the 2nd
argument as in

unsplit(l, rep(seq(along=l), sapply(l,length)) )

but if you think about it, it's not really doing anything that
do.call("c",l) or do.call("rbind",l) won't do.

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list