How to combine two variables according to levels of a factor in R

factors, r, variables

Solution

using `ifelse`(slow, but easy):

data_sample <- transform(data_sample, 
                         wanted = ifelse(color == "red", 
                                         value1, 
                                         ifelse(color == "blue", 
                                                value2, 
                                                NA)))

or

data_sample <- transform(data_sample, 
                         wanted = ifelse(color == "red", 
                                         value1, 
                                         value2))

if there are only those two colors.

Problem

I am struggling with a question, which is probably really basic, but I am not able to find a solution. I would greatly appreciate any help. I have a dataframe containing two variables, which I would like to merge in the same variable. The dataframe looks something like this: ``` id <- 1:6 color <- c(rep("red", 3), "blue", "red", "blue") value2 <- 20:25 value1 <- 25:30 wanted_outcome <- c(25,26,27,23,29,25) data_sample <- data.frame(id, color, value1, value2, wanted_outcome) data_sample id color value1 value2 wanted_outcome 1 1 red 25 20 25 2 2 red 26 21 26 3 3 red 27 22 27 4 4 blue 28 23 23 5 5 red 29 24 29 6 6 blue 30 25 25 ``` The outcome that I want is in the last column. Basically I would like to create a new variable, which contains the values from the variable value1 for red items and the values from value2 for blue items. This is what I am trying, however, it is not producing the desired result, as R is replacing the values starting from the first one and not row by row. ``` data_sample$value_combined[color=="red"] <- value1 data_sample$value_combined[color=="blue"] <- value2 data_sample id color value1 value2 wanted_outcome value_combined 1 1 red 25 20 25 25 2 2 red 26 21 26 26 3 3 red 27 22 27 27 4 4 blue 28 23 23 20 5 5 red 29 24 29 28 6 6 blue 30 25 25 21 ``` Any help would be appreciated. Thanks in advance.

Original source