使用 mutate_all 时如何在 case_when 内添加列名?
How to add a column name inside a case_when when using mutate_all and across?
我有以下数据框,我想改变 ENV
数据框中的所有列,以用 case_when
函数替换一些值:
metrics <-
structure(list(var = c("SST", "SSS"), min = c(-1.82623172259469,
0.0106425614590989), max = c(30.7357019713913, 62.4178951570953
), Q1 = c(29.6467844102333, 37.2283509056568), Q2 = c(25.0978897764037,
35.1366150711973), Q3 = c(1.08054073780591, 33.6817701728394),
Q4 = c(-1.75609879755353, 22.3324745864139), slope.L = c(-0.219833625638,
-0.478071840394898), slope.R = c(0.352529811255446, 0.0881111953059067
)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))
ENV1<-
structure(list(SST = c(-1.18181673490857, -1.29487169195505,
-1.2891296530063, -1.16459061806231, -1.02749131873705, -0.877831755030538,
-0.827006290249864, -0.87501492439503, -0.907842479642887, -0.925488955993432,
-0.928734689365441), SSS = c(34.4828464105725, 34.4536828344067,
34.4394813115398, 34.4402418419719, 34.4202143674095, 34.3793988878528,
34.3479811742902, 34.3259612267216, 34.2944239596526, 34.2533693730831,
34.225624174873)), row.names = 30:40, class = "data.frame")
ENV1 |> mutate_all(funs(
case_when(
. < metrics[metrics$var == x,]$Q1 ~ 0,
. <= metrics[metrics$var == x,]$Q3 ~ 1,
. > metrics[metrics$var == x,]$Q4 ~ 0
)
))
我对用什么代替 x
有疑问?我尝试使用 names(.)
,列的名称(因此它从 metrics
数据框中获取值。但它不起作用。我还尝试了 colnames(.)、vars(.)、as.character(.) - 运气不好
尝试cur_column()
ENV1 %>%
mutate(across(everything(),
~case_when(
. < metrics[metrics$var == cur_column(),]$Q1 ~ 0,
. <= metrics[metrics$var == cur_column(),]$Q3 ~ 1,
. > metrics[metrics$var == cur_column(),]$Q4 ~ 0
)
))
我有以下数据框,我想改变 ENV
数据框中的所有列,以用 case_when
函数替换一些值:
metrics <-
structure(list(var = c("SST", "SSS"), min = c(-1.82623172259469,
0.0106425614590989), max = c(30.7357019713913, 62.4178951570953
), Q1 = c(29.6467844102333, 37.2283509056568), Q2 = c(25.0978897764037,
35.1366150711973), Q3 = c(1.08054073780591, 33.6817701728394),
Q4 = c(-1.75609879755353, 22.3324745864139), slope.L = c(-0.219833625638,
-0.478071840394898), slope.R = c(0.352529811255446, 0.0881111953059067
)), row.names = c(NA, -2L), class = c("tbl_df", "tbl", "data.frame"
))
ENV1<-
structure(list(SST = c(-1.18181673490857, -1.29487169195505,
-1.2891296530063, -1.16459061806231, -1.02749131873705, -0.877831755030538,
-0.827006290249864, -0.87501492439503, -0.907842479642887, -0.925488955993432,
-0.928734689365441), SSS = c(34.4828464105725, 34.4536828344067,
34.4394813115398, 34.4402418419719, 34.4202143674095, 34.3793988878528,
34.3479811742902, 34.3259612267216, 34.2944239596526, 34.2533693730831,
34.225624174873)), row.names = 30:40, class = "data.frame")
ENV1 |> mutate_all(funs(
case_when(
. < metrics[metrics$var == x,]$Q1 ~ 0,
. <= metrics[metrics$var == x,]$Q3 ~ 1,
. > metrics[metrics$var == x,]$Q4 ~ 0
)
))
我对用什么代替 x
有疑问?我尝试使用 names(.)
,列的名称(因此它从 metrics
数据框中获取值。但它不起作用。我还尝试了 colnames(.)、vars(.)、as.character(.) - 运气不好
尝试cur_column()
ENV1 %>%
mutate(across(everything(),
~case_when(
. < metrics[metrics$var == cur_column(),]$Q1 ~ 0,
. <= metrics[metrics$var == cur_column(),]$Q3 ~ 1,
. > metrics[metrics$var == cur_column(),]$Q4 ~ 0
)
))