[英]How to add a new column using mutate function from a group of existing columns with similar names
I would like to add a column to my data frame based upon the values in other columns.我想根据其他列中的值向我的数据框中添加一列。
Here is an extract of the data.这是数据的摘录。
On each row if any of the 4 TOPER columns have any of the following values (92514, 92515, 92508, 92510, 92511 or 92512( i want the S_Flag
column to be equal to 1, If not the S_Flag
value should be 0. Have highlighted the data where this true (case nos 2, 4, 6 and 8) - therefore S_Flag
should be made 1. Have tried using a ifelse
inside a mutate
function. Just not sure how to identify looking across all 4 TOPER columns within the ifelse
function??? Have tried在每一行上,如果 4 个 TOPER 列中的任何一个具有以下任何值(92514、92515、92508、92510、92511 或 92512(我希望S_Flag
列等于 1,如果不是,则S_Flag
值应该为 0。有突出显示正确的数据(第 2、4、6 和 8 号案例)-因此应制作S_Flag
1。已尝试在mutate
function 中使用ifelse
。只是不确定如何识别查看 ifelse 中的所有 4 个ifelse
列function??? 试过
tt <- mutate(rr, S_Flag = ifelse( any(vars(everything()) %in% toper_vec), 1,0))
where rr
is the original data frame and toper_vec
is a vector containing the 6 TOPER column values.其中rr
是原始数据框, toper_vec
是包含 6 个 TOPER 列值的向量。
Hope that makes sense.希望这是有道理的。 By the way i am in early stages of learning R.顺便说一句,我正处于学习 R 的早期阶段。 Thank you for any assistance.感谢您提供任何帮助。
A couple of quick fixes should make your code work: (1) use rowwise() and (2) use across().几个快速修复应该使您的代码工作:(1)使用 rowwise() 和 (2) 使用 cross()。
The revised code reads:修改后的代码如下:
tt <- rr %>%
rowwise() %>%
mutate(S_Flag = if_else( any(across(everything()) %in% toper_vec), 1,0))
A similar question was addressed in the following informative post: Check row wise if value is present in column and update new column row wise以下内容丰富的帖子中解决了类似的问题: Check row wise if value is present in column and update new column row wise
Applying the suggested approach in that post to your immediate question, the following should work:将该帖子中建议的方法应用于您的直接问题,以下应该有效:
library(tidyverse)
toper_vec <- c(92514, 92515, 92508, 92510, 92511, 92512)
df <- data.frame("CASE" = c(1, 2, 3, 4, 5),
"TOPER1" = c(86509, 92514, 87659, 45232, 86509),
"TOPER2" = c(12341, 10094, 12341, 92508, 10094),
"TOPER3" = c(86509, 67326, 41908, 50567, 50567))
new_df <- df %>%
rowwise() %>%
mutate(S_Flag = case_when(TOPER1 %in% toper_vec ~ 1,
TOPER2 %in% toper_vec ~ 1,
TOPER3 %in% toper_vec ~ 1,
TRUE ~ 0))
Here's an alternative, reusing toper_vec
and df
from Blue050205:这是一个替代方案,重用toper_vec
中的 toper_vec 和df
:
df %>%
rowwise() %>%
mutate(s_flag = if_else(any(c_across(starts_with("TOP")) %in% toper_vec), 1, 0))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.