[英]How to combine two variables according to levels of a factor in R
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. 基本上我想创建一个新变量,它包含红色项的变量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. 提前致谢。
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. 如果只有那两种颜色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.