[英]Creating a new column based on the values of other two columns in r
我的数据集有以下问题,我有一列存储参与者的选择左侧或右侧,另外两列存储左右选项代表的内容。
例如,如果第一列等于 1(左),而其他两列存储 left = Masked Picture,right = Unmaksed Picture。 所以,在这种情况下,我会知道这个参与者选择了蒙面图片。
Main_task Left_option Right_option (the column I want to create)
1(Left) Masked Unmasked Masked
2(Right) Unmasked Masked Masked
1(Left) Unmasked Masked Unmasked
2(Right) Masked Unmasked Unmasked
2(Right)
由于我有一个大型数据集,我想知道如何根据这些列创建一个新列?
您的帮助将不胜感激!
谢谢
一个可能的解决方案:
library(dplyr)
df %>%
mutate(new = if_else(Main_task == "1(Left)", Left_option, Right_option))
#> Main_task Left_option Right_option new
#> 1 1(Left) Masked Unmasked Masked
#> 2 2(Right) Unmasked Masked Masked
#> 3 1(Left) Unmasked Masked Unmasked
#> 4 2(Right) Masked Unmasked Unmasked
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.