gpt4 book ai didi

r - 在 dplyr 1.0.0 中使用 mutate() 和 across() 从多个变量创建新变量

转载 作者:行者123 更新时间:2023-12-02 16:10:01 26 4
gpt4 key购买 nike

我需要以相同的方式将所有具有相同前缀的多个列转变为新列。

这是玩具数据

df <- data.frame(su_1 = round(rnorm(12),2),
su_2 = round(rnorm(12),2),
su_3 = round(rnorm(12),2))

现在假设我想将每个变量的连续值分类到离散的 bin 中。我可以像这样对每一列使用三个独立的类似步骤来完成此操作

df %>% mutate(su_1_disc = ifelse(su_1 < 0, "less", 
ifelse(su_1 > 0 & su_1 <= 0.5, "mid", "lots"))) -> df

df %>% mutate(su_2_disc = ifelse(su_2 < 0, "less",
ifelse(su_2 > 0 & su_2 <= 0.5, "mid", "lots"))) -> df

df %>% mutate(su_3_disc = ifelse(su_3 < 0, "less",
ifelse(su_3 > 0 & su_3 <= 0.5, "mid", "lots"))) -> df

df

# output
# su_1 su_2 su_3 su_1_disc su_2_disc su_3_disc
# 1 1.99 0.77 -0.17 lots lots less
# 2 0.51 -0.76 -1.24 lots less less
# 3 1.50 -0.36 0.28 lots less mid
# 4 0.86 0.88 -0.52 lots lots less
# 5 0.08 0.63 -0.76 mid lots less
# 6 -0.51 -0.99 0.01 less less mid
# 7 0.35 1.59 0.19 mid lots mid
# 8 0.16 0.35 0.38 mid mid mid
# 9 -0.75 -0.45 1.75 less less lots
# 10 0.97 0.62 -0.05 lots lots less
# 11 -0.07 0.47 -0.24 less mid less
# 12 0.61 -0.27 -1.55 lots less less

但我想使用新的 dplyr 1.0.0 功能一步完成

我试过了

df %>%
mutate(across(starts_with("su_"),
ifelse(.x < 0, "less",
ifelse(.x > 0 & .x <= 0.5, "mid", "lots"))))

但是它抛出了一个错误。我知道 .names 需要在某个地方出现,但我有点迷路了。

最佳答案

你可以使用-

library(dplyr)

df %>%
mutate(across(starts_with("su_"),~ifelse(.x < 0, "less",
ifelse(.x > 0 & .x <= 0.5, "mid", "lots")), .names = '{col}_disc'))

# su_1 su_2 su_3 su_1_disc su_2_disc su_3_disc
#1 0.40 0.57 -0.11 mid lots less
#2 1.82 -0.55 0.44 lots less mid
#3 0.44 1.47 -0.39 mid lots less
#4 -0.82 0.00 -0.12 less lots less
#5 0.17 -0.10 -1.55 mid less less
#6 0.20 0.98 -1.02 mid lots less
#7 -0.01 1.12 -0.30 less lots less
#8 -0.70 0.31 0.35 less mid mid
#9 0.46 1.18 -0.22 mid lots less
#10 -1.09 0.03 -0.85 less mid less
#11 -0.03 1.81 1.28 less lots lots
#12 -0.11 1.64 -0.51 less lots less

您还可以将 ifelse 替换为 case_whencut

关于r - 在 dplyr 1.0.0 中使用 mutate() 和 across() 从多个变量创建新变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68263951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com