[R] struggling with "split" function

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Sun Sep 6 11:43:35 CEST 2009


one way is the following:

ind <- rle(is.na(x$A))
ind <- rep(seq_along(ind$lengths), ind$lengths)
na.ind <- is.na(x$A)
split(x[na.ind, -1], ind[na.ind])


I hope it helps.

Best,
Dimitris


Dimitri Liakhovitski wrote:
> I am very sorry for such a simple question, but I am struggling with "split".
> I have the following data frame:
> x<-data.frame(A=c(NA,NA,NA,NA,"split",NA,NA,NA,NA,"split",NA,NA,NA,NA,"split",NA,NA,NA,NA),
> B=c("Name1","text1","text2","text3",NA,"Name2","text1","text2","text3",NA,"Name3","text1","text2","text3",NA,"Name4","text1","text2","text3"),
> C=c(NA,1,NA,3,NA,NA,4,5,6,NA,NA,7,8,9,NA,NA,3,3,3),D=c(NA,1,1,2,NA,NA,5,6,NA,NA,NA,9,8,7,NA,NA,2,2,2),
> E=c(NA,3,2,1,NA,NA,6,5,4,NA,NA,7,7,8,NA,NA,1,NA,1))
> print(x)
> 
> All I want to do is to split x, i.e., to create a list of data frames
> that are currently separated by the word "split" in column A. In this
> example, it would be 4 data frames, the first of them being:
> A B C D E
> NA Name1 NA NA NA
> NA text1 1 1 3
> NA text 2 NA 1 2
> NA text3 3 2 1
> 
> etc.
> 
> I tried:
> split(x, x$A)
> split(x,x$A == 'split')
> split(x,!is.na(x$A))
> 
> But nothing produces what I need.
> Tanks a lot for any hint!
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list