[R] For loops

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Tue Apr 10 00:20:45 CEST 2012


class1 <- data[1==data$class,]

gets you a subset of the data into a dedicated object, but if you want to handle arbitrarily large amounts of data or class values then the list output of split is really much better to stay with.

Also, "data" is a predefined function, so it is not a good idea to define objects with that name due to confusing code review.
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

Christopher Desjardins <cddesjardins at gmail.com> wrote:

>Hi,
>I am having trouble with syntax for a for loop. Here is what I am
>trying to
>do.
>
>class=c(rep(1,3),rep(2,3),rep(3,3))
>out1=rnorm(length(class))
>out2=rnorm(length(class))
>out3=rnorm(length(class))
>data=data.frame(class,out1,out2,out3)
>
>dat.split=split(data,data$class)
>  for(i in 1:3){
>  sub[i]=dat.split[i]
>  }
>
>However, the for loop doesn't work. I want to assign each split to a
>different data object. Better yet, how I could assign each class to a
>separate object and skip the splitting?
>
>Thanks,
>Chris
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>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.



More information about the R-help mailing list