我想有条件地用两个不同的因子乘以值,如果值< 7,则乘以10,如果值为>= 7,则乘以5。
我试着使用case_when内部的变异和跨越,但它没有工作。
library(tidyverse)
df <- as_tibble(iris)
df_new <- df %>%
mutate(across(1:4, ~ case_when(. < 7 ~ * 10, . >= 7 ~ * 5)))case_when()甚至可以做到这一点吗?
发布于 2022-07-20 20:07:17
library(dplyr)
iris %>%
mutate(across(1:4, ~ case_when(.x < 7 ~ .x * 10,
.x >= 7 ~ .x * 5))) %>%
head(10)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 51 35 14 2 setosa
#> 2 49 30 14 2 setosa
#> 3 47 32 13 2 setosa
#> 4 46 31 15 2 setosa
#> 5 50 36 14 2 setosa
#> 6 54 39 17 4 setosa
#> 7 46 34 14 3 setosa
#> 8 50 34 15 2 setosa
#> 9 44 29 14 2 setosa
#> 10 49 31 15 1 setosahttps://stackoverflow.com/questions/73056524
复制相似问题