Dear R Studio Community,
I've currently been working with a data set like this:
df <- data.frame(var1 <- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4), var2 <- c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4))
I've been trying to create a new variable using mutate() and if_else(). The idea is that the new variable gets assigned to the two existing variables var1 and var2 by different value combinations, meaning:
var3 = 1 IF var1 = 1 and var2 = 2 OR var1 = 3 and var2 = 4
var3 = 2 IF var1 = 1 and var2 = 3 OR var1 = 2 and var2 = 4
The other combinations are not of importance and can result in "NA"
I've been trying to create the new variable like this:
df$var3 <- mutate(var3 = if_else(df$var1 == 1 && df$var2 == 2 | df$var1 == 3 && df$var2 == 4), "1", if_else(df$var1 == 1 && df$var2 == 3 | df$var1 == 2 && df$var2 == 4 ), "2")
But the problem "no applicable method for 'mutate_' applied to an object of class "character"" keeps popping up.
I'm still quite new to R so I'm not really sure where this issue is coming from and whether my method even makes sense to begin with. I hope that you understand my idea behind all this and can help me with this issue!