Ignacio Ignacio - 3 months ago 13
R Question

Merge 2 variables into 1?

Suppose my original data looks something like this

df <- data.frame(id = 1:10,
V = LETTERS[1:10],
Treatment1 = c(rep(1,3), rep(0,7)),
Treatment2 = c(rep(0,3), rep(1,3), rep(0,4)))


I would like to merge
Treatment1
and
Treatment2
into a new variable that takes 1 of 3 values:
Treatment1
,
Treatment2
,
Control
. That is I would like to end up with this data frame:

df2 <- data.frame(id = 1:10,
V = LETTERS[1:10],
Treatment = c(rep("Treatment1",3),
rep("Treatment2",3),
rep("Control",4)))


Right now I'm doing it with this code:

library(dplyr)
df$Treatment <- ifelse(test = df$Treatment1==1, yes = "Treatment1",
no = ifelse(test = df$Treatment2==1,
yes = "Treatment2", no = "Control"))

df2 <- df %>% select(-Treatment1, -Treatment2)


Is there a better way?

Answer

One way to do it that ends up being reasonably readable and extendable is to create a lookup table and merge it with your existing data as follows:

df2 <- data.frame(Treatment1 = c(1,0,0),
                  Treatment2 = c(0,1,0),
                  Treatment = c("Control", "Treatment1", "Treatment2"));
merge(df, df2, all.x=TRUE)  #Setting all.x ensures rows of df aren't dropped if there isn't a match

 #      Treatment1 Treatment2 id V  Treatment
 #   1           0          0  7 G Treatment2
 #   2           0          0  8 H Treatment2
 #   3           0          0  9 I Treatment2
 #   4           0          0 10 J Treatment2
 #   5           0          1  4 D Treatment1
 #   6           0          1  5 E Treatment1
 #   7           0          1  6 F Treatment1
 #   8           1          0  1 A    Control
 #   9           1          0  2 B    Control
 #   10          1          0  3 C    Control
Comments