Dpylr 的 recode 函数 multiple-to-1 R
Dpylr's recode function multiple-to-1 R
我想要一种更简单的方法来重新编码向量。具体来说,我想知道是否有办法将向量传递给像 dplyr
的 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)))
}
最后,我知道一个基本解决方案。
vec3 <- vec1
val1 <- c("A", "B")
val2 <- c("C", "D")
vec3[vec1 %in% val1] <- "Value1"
vec3[vec1 %in% val2] <- "Value2"
vec3[!vec1 %in% c(val1,val1)] <- "Value other"
但我不知道如何将此处执行的赋值合并到一个函数中。
我们可以使用 dplyr
包中的 case_when
。
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"
使用 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")
我想要一种更简单的方法来重新编码向量。具体来说,我想知道是否有办法将向量传递给像 dplyr
的 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)))
}
最后,我知道一个基本解决方案。
vec3 <- vec1
val1 <- c("A", "B")
val2 <- c("C", "D")
vec3[vec1 %in% val1] <- "Value1"
vec3[vec1 %in% val2] <- "Value2"
vec3[!vec1 %in% c(val1,val1)] <- "Value other"
但我不知道如何将此处执行的赋值合并到一个函数中。
我们可以使用 dplyr
包中的 case_when
。
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"
使用 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")