Re: [R] How to split DF into a list and avoid NULL elements in this list

From: <Jagat.K.Sheth_at_wellsfargo.com>
Date: Tue, 25 Nov 2008 09:15:06 -0600

Here's one way

x <- sapply(seq(1,ncol(DF),by=2), function(i) DF[,i:(i+1)], simplify=F)

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Lauri Nikkinen
Sent: Tuesday, November 25, 2008 9:00 AM To: r-help_at_stat.math.ethz.ch
Subject: [R] How to split DF into a list and avoid NULL elements in this list

Hello,

I'm trying to split my DF into a list using incremental loop. How can I avoid NULL elements in this list?

DF <- data.frame(var1 = 1:10, var2 = 11:20, var3 = 21:30, var4 = 31:40)

x <- list()
i <- 1

while (i <= ncol(DF)-1) {

          x[[i]] <- DF[, i:c(i+1)]
          i <- i + 2

}
x

Many thanks,
Lauri



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Tue 25 Nov 2008 - 15:19:51 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 25 Nov 2008 - 15:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive