如何根据R中的一个因子的水平组合两个变量

时间:2021-02-04 14:56:56

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:

我有一个包含两个变量的dataframe,我想将它们合并到同一个变量中。dataframe看起来是这样的:

    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.

我想要的结果在最后一列。基本上,我想要创建一个新变量,它包含红色项的变量value1的值和蓝色项的值value2的值。

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.

这是我正在尝试的,然而,它并没有产生期望的结果,因为R正在替换从第一个开始的值,而不是逐行替换。

   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.

如有任何帮助,我们将不胜感激。提前谢谢。

1 个解决方案

#1


4  

using ifelse(slow, but easy):

使用ifelse(慢,但容易):

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.

如果只有这两种颜色。

#1


4  

using ifelse(slow, but easy):

使用ifelse(慢,但容易):

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.

如果只有这两种颜色。