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

Tina Chatterjee t|n@mun|m2019 @end|ng |rom gm@||@com
Thu Jun 13 19:30:54 CEST 2019


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]]



More information about the R-help mailing list