[R] Merging a dataframe after subsetting with respect to several factors

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Fri Jun 14 04:00:46 CEST 2019


Jeff:
Your solution is not quite what she asked for (she wanted a data frame, not
a list).

Moreover, most of the time it is done automatically as the first step of a
tapply() /filter() type operation or is inherent in modeling and
trellis-type plots. I *still* suspect it is unnecessary, but of course I
could be wrong. That's why I asked for clarification.

Cheers,
Bert

On Thu, Jun 13, 2019 at 5:34 PM Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
wrote:

> I do it regularly.
>
> Base R:
>
> result <- split( DF[ , 4, drop=FALSE ], DF[ , -4 ] )
>
> Tidyverse:
>
> library(tidyr)
> result <- nest( DF, time )
> filter( result, "a2"==a & "b1"==b & "c1"==c )[[ "data" ]]
>
> On Thu, 13 Jun 2019, Bert Gunter wrote:
>
> > Why? I suspect that there is no reason that you need to do this.
> >
> > Cheers,
> > Bert
> >
> > Bert Gunter
> >
> > "The trouble with having an open mind is that people keep coming along
> and
> > sticking things into it."
> > -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> >
> >
> > On Thu, Jun 13, 2019 at 1:22 PM Tina Chatterjee <tinamunim2019 using gmail.com
> >
> > wrote:
> >
> >> Hello everyone!
> >> I have the following dataframe(df).
> >>
> >> a<-c("a1","a2","a2","a1","a1","a1")
> >> b<-c("b1","b1","b1","b1","b1","b2")
> >> c<-c("c1","c1","c1","c1","c1","c2")
> >> time <- c(runif(6,0,1))
> >>
> >> df<-data.frame(a,b,c,time)
> >> df
> >>
> >>    a  b  c       time
> >> 1 a1 b1 c1 0.28781082
> >> 2 a2 b1 c1 0.02102591
> >> 3 a2 b1 c1 0.72479220
> >> 4 a1 b1 c1 0.41947675
> >> 5 a1 b1 c1 0.58899855
> >> 6 a1 b2 c2 0.82414123
> >>
> >> Now, I want to extract the time components corresponding
> >> to the specific combination of the factors. Finally I have made a
> dataframe
> >> (df_1) with 2 columns one with the time components and the other with
> the
> >> level combinations.
> >>
> >> df[df$a=="a1" & df$b=="b1" & df$c=="c1",]$time
> >> df[df$a=="a2" & df$b=="b1" & df$c=="c1",]$time
> >> df[df$a=="a1" & df$b=="b2" & df$c=="c2",]$time
> >>
> >> val <- c(df[df$a=="a1" & df$b=="b1" & df$c=="c1",]$time,df[df$a=="a2" &
> >> df$b=="b1" & df$c=="c1",]$time,df[df$a=="a1" & df$b=="b2" &
> >> df$c=="c2",]$time)
> >> name <- c(rep("a1b1c1",3),rep("a2b1c1",2),"a1b2c2")
> >> df_1 <- data.frame(val,name)
> >>
> >> I made it manually. In reality I have a lot of treatment combinations.
> So,
> >> could you please suggest how can I do this with a loop or any control
> >> sequence?
> >> Thanks and regards.
> >> Tina
> >>
> >>         [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> >>
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > 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.
> >
>
> ---------------------------------------------------------------------------
> Jeff Newmiller                        The     .....       .....  Go Live...
> DCN:<jdnewmil using 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
> ---------------------------------------------------------------------------
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list