我想改变包含字符串“account”的多个列。具体来说,我希望这些列在满足某个条件时采用“NA”,在不满足条件时采用另一个值。下面我将介绍我的尝试,灵感来自 here和 here 。到目前为止,还没有成功。仍在尝试,但任何帮助将不胜感激。
我的数据
df<-as.data.frame(structure(list(low_account = c(1, 1, 0.5, 0.5, 0.5, 0.5), high_account = c(16,
16, 56, 56, 56, 56), mid_account_0 = c(8.5, 8.5, 28.25, 28.25,
28.25, 28.25), mean_account_0 = c(31.174, 30.1922101449275, 30.1922101449275,
33.3055555555556, 31.174, 33.3055555555556), median_account_0 = c(2.1,
3.8, 24.2, 24.2, 24.2, 24.2), low_account.1 = c(1, 1, 0.5, 0.5, 0.5,
0.5), high_account.1 = c(16, 16, 56, 56, 56, 56), row.names = c("A001", "A002", "A003", "A004", "A005", "A006"))))
df
low_account high_account mid_account_0 mean_account_0 median_account_0 low_account.1 high_account.1 row.names
1 1.0 16 8.50 31.17400 2.1 1.0 16 A001
2 1.0 16 8.50 30.19221 3.8 1.0 16 A002
3 0.5 56 28.25 30.19221 24.2 0.5 56 A003
4 0.5 56 28.25 33.30556 24.2 0.5 56 A004
5 0.5 56 28.25 31.17400 24.2 0.5 56 A005
6 0.5 56 28.25 33.30556 24.2 0.5 56 A006
我的尝试
sample_data<-df%>% mutate_at(select(contains("account") , ifelse(. <= df$low_account& >= df$high_account, NA, .)))
Error: No tidyselect variables were registered Call
rlang::last_error()
to see a backtrace
预期输出
df
low_account high_account mid_account_0 mean_account_0 median_account_0 low_account.1 high_account.1 row.names
1 1.0 16 8.50 NA 2.1 1.0 16 A001
2 1.0 16 8.50 NA 3.8 1.0 16 A002
3 0.5 56 28.25 30.19221 24.2 0.5 56 A003
4 0.5 56 28.25 33.30556 24.2 0.5 56 A004
5 0.5 56 28.25 31.17400 24.2 0.5 56 A005
6 0.5 56 28.25 33.30556 24.2 0.5 56 A006
最佳答案
vars(contains('account'))
的问题在于它匹配存在子字符串“account”的所有列,并且当我们进行逻辑比较时,“low_account”会匹配列被转换为 NA
因为它肯定低于或等于 'low_account',因此只有 NA 替换的列可用。因此,我们可以获取感兴趣的“mid”、“median”、“mean”列,然后进行replace
library(tidyverse)
df %>%
mutate_at(vars(matches("(mid|mean|median)_account")),
~ replace(., .<= low_account | .>= high_account, NA))
# low_account high_account mid_account_0 mean_account_0 median_account_0 low_account.1 high_account.1 row.names
#1 1.0 16 8.50 NA 2.1 1.0 16 A001
#2 1.0 16 8.50 NA 3.8 1.0 16 A002
#3 0.5 56 28.25 30.19221 24.2 0.5 56 A003
#4 0.5 56 28.25 33.30556 24.2 0.5 56 A004
#5 0.5 56 28.25 31.17400 24.2 0.5 56 A005
#6 0.5 56 28.25 33.30556 24.2 0.5 56 A006
关于r - 如何使用 "contains"和 "ifelse"有条件地改变多个列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57043794/