r - 如何使用 "contains"和 "ifelse"有条件地改变多个列?

标签 r function tidyverse

我想改变包含字符串“account”的多个列。具体来说,我希望这些列在满足某个条件时采用“NA”,在不满足条件时采用另一个值。下面我将介绍我的尝试,灵感来自 herehere 。到目前为止,还没有成功。仍在尝试,但任何帮助将不胜感激。

我的数据

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/

相关文章:

php - 如何获取可选参数的默认值

R 中的阈值舍入数字

r - 如何以整洁的方式重新排序因子水平?

r - 将文字描述放在 kable 旁边

python - 为什么 Python 和 R 有两个不同的归一化结果

r - 将多个函数应用于矩阵列表并在数据框中输出答案

function - 在Powershell中使用哈希表格式化属性

r - 使用 dplyr 匹配两个数据帧中的时间戳

regex - R中位置序列的长度(以字符串形式给出)

r - 来回到 R 中的虚拟变量