我想要一种更简单的方法来重新编码矢量。具体来说,我想知道是否有办法将向量传递给像dplyr
的{{1}}这样的函数。我理解quasiquotation的基础知识,但不太了解如何合并recode
。
=
我想做以下
library(tidyverse)
vec1 <- rep(LETTERS[1:7],7)
#standard way
vec2 <- recode(vec1,
"A" = "Value1",
"B" = "Value2",
"C" = "Value3",
"D" = "Value4",
"E" = "Value5",
"F" = "Value6",
"G" = "Value7"
)
vec3 <- recode(vec1,
"A" = "Value1",
"B" = "Value1",
"C" = "Value2",
"D" = "Value2",
.default = "Value other"
)
我有一个解决方案,但无法弄清楚如何将功能合并到vec3 <- some.function(vec1,
c("A", "B") = "Value1",
c("C", "D") = "Value2",
.default = "Value other"
)
和...
=
我也找到了传递两个向量并重命名所有变量的方法。
do.call(dplyr::recode,
c(list(vec1),
setNames(rep("Value1",length(val1)), val1),
setNames(rep("Value2",length(val2)), val2)))
最后,我知道一个基本解决方案。
recode.by.vectors <- function(x, current.names, new.names){
do.call(dplyr::recode, c(list(x), setNames(new.names, current.names)))
}
但我不知道如何将此处执行的分配合并到一个函数中。
答案 0 :(得分:3)
我们可以使用case_when
包中的dplyr
。
library(dplyr)
vec1 <- rep(LETTERS[1:7],7)
vec2 <- case_when(
vec1 %in% c("A", "B") ~ "Value1",
vec1 %in% c("C", "D") ~ "Value2",
TRUE ~ "Value other"
)
head(vec2)
# [1] "Value1" "Value1" "Value2" "Value2" "Value other" "Value other"
答案 1 :(得分:3)
使用forcats
包(也包含在tidyverse
包中)
library(forcats)
vec1 <- rep(LETTERS[1:7], 7)
fct_collapse(vec1,
Value1 = c("A", "B"),
Value2 = c("C", "D"),
`Value other` = c("E", "F", "G"))
如果您有很多类别要放入Value other
,这有点麻烦,但是通过辅助步骤,您可以稍微简化一下
fct_collapse(vec1,
Value1 = c("A", "B"),
Value2 = c("C", "D")) %>%
fct_other(keep = c("Value1", "Value2"),
other_level = "Value other")