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]]
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.
On Apr 9, 2012, at 5:33 PM, Christopher Desjardins 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.Why? What's wrong with leaving them in a list that split() provides. In that form you can easily use lapply() and start your 12 step program away from for-loop addiction.> Better yet, how I could assign each class to a > separate object and skip the splitting? >-- David Winsemius, MD West Hartford, CT
In addition to what Jeff and David have said...
If you really want to create a separate data frame for each subgroup then
you need use the assign function, and also data.split[[i]] instead of
data.split[1].
for (i in 1:3) {
assign( paste('sub',i,sep='') , data.split[[i]]
}
Jeff showed you how to skip the splitting, by subsetting within the loop.
'class' is also a R built-in function name, and thus not a good choice
for
one's on use.
-Don
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 4/9/12 2:33 PM, "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.
Possibly Parallel Threads
- [PATCH 3/6] Input: Update vmmouse.c to use the common VMW_PORT macros
- [PATCH 3/6] Input: Update vmmouse.c to use the common VMW_PORT macros
- Lots of huge matrices, for-loops, speed
- [PATCH 3/6] Input: Update vmmouse.c to use the common VMW_PORT macros
- [PATCH 3/6] Input: Update vmmouse.c to use the common VMW_PORT macros