一段时间以来,我一直在创建长达20多个行的重命名case_when()
语句,以基于后一列的值来创建新列。
现在,它看起来像这样:
tibble(x = rep(1:3, 3),
y = case_when(x == 1 ~ "good",
x == 2 ~ "good",
x == 3 ~ "bad",
TRUE ~ as.character(x)))
但是对于一长串变量,像这样重命名/创建新的cols完全无效。一个实例具有3-6个变量的4个潜在赋值(上面的col y
),因此重复很多。
任何有关如何使此过程更有效的资料/想法?
答案 0 :(得分:1)
这就是您要寻找的。我正在使用mutate_at
同时将您的分类应用于4列:
库(dplyr)
tibble(x1 = rep(1:3, 3),
x2 = rep(3:1, 3),
x3 = rep(1:3, each = 3),
x4 = rep(3:1, each = 3)) %>%
mutate_at(vars(c("x1", "x2", "x3", "x4")),
list(y = ~case_when(. %in% 1:2 ~ "good",
. == 3 ~ "bad",
TRUE ~ as.character(.))))
# A tibble: 9 x 8
x1 x2 x3 x4 x1_y x2_y x3_y x4_y
<int> <int> <int> <int> <chr> <chr> <chr> <chr>
1 1 3 1 3 good bad good bad
2 2 2 1 3 good good good bad
3 3 1 1 3 bad good good bad
4 1 3 2 2 good bad good good
5 2 2 2 2 good good good good
6 3 1 2 2 bad good good good
7 1 3 3 1 good bad bad good
8 2 2 3 1 good good bad good
9 3 1 3 1 bad good bad good