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

Richard O'Keefe r@oknz @end|ng |rom gm@||@com
Thu Jun 13 22:34:27 CEST 2019


How about just
  df$time[match(paste(df$a, df$b, df$c), c(
    "co mb o1",
    ..
    "co mb oN"))]



On Fri, 14 Jun 2019 at 08:22, 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]]



More information about the R-help mailing list