R中是否有一个函数可以同时改变几个相似的因子水平?
Is there a function in R to change several similar factor levels at once?
我在堆栈溢出时的第二个问题,欢迎所有提示:)
对于临床研究,我必须重新编码许多二分基线特征,其中包含“是”和“否”的多种变体。
目前我正在一个一个地重新编码这些变量,但是它需要很多行代码并且所有不同变量之间的变化非常相似。在未知或 NA 的情况下,我想重新编码为 0。
例子
library(dplyr)
A <- c("Yes", "y", "no", "n", "UK")
B <- c("yes", "Yes", "y", "no", "no")
C <- c("Y", "y", "n", "no", "uk")
#attempt 1 was to recode all variables one by one
A <- recode(A, "Yes" = "yes", "y" = "yes", "n" = "no", "UK" = "no")
B <- recode (B, "Yes" = "yes", "y" = "yes")
C <- recode(C, "Y" = "yes", "y" = "yes", "n" = "no", "uk" = "no")
#attempt 2 was to use a list option on all vectors.
levels(A) <- list("yes"=c("Likely", "y", "Y", "Yes", "yes"), "no" = c("", "No", "UK", "no", "N", "n"))
我想知道是否有一种方法可以在包含所有 A、B、C 的 list/vector 上执行此列表选项?或者也许有另一种方法可以更轻松、更有效地重新编码这些变量?
任何帮助都会很棒 :)
如果向量长度相同,您可以将它们放入数据框中,或者如果它们长度不同,则将它们放入列表中,然后使用 lapply
对所有向量应用相同的函数。您可以使用 forcats::fct_collapse
将多个级别合并为一个级别。
list_vec <- list(A, B, C)
list_vec <- lapply(list_vec, function(x) forcats::fct_collapse(x,
"yes"=c("Likely", "y", "Y", "Yes", "yes"),
"no" = c("", "No", "UK", "no", "N", "n", "uk")))
您可以从向量中使用 grepl
到 select yes
或 no
。
c("0","no","yes")[1 + grepl("^no?", A, TRUE) + 2*grepl("^ye?s?", A, TRUE)]
#[1] "yes" "yes" "no" "no" "0"
要对许多向量进行此操作,您可以使用如下循环:
for(x in c("A","B","C")) {
assign(x, c("0","no","yes")[1 + grepl("^no?", get(x), TRUE) +
2*grepl("^ye?s?", get(x), TRUE)])
}
A
#[1] "yes" "yes" "no" "no" "0"
B
#[1] "yes" "yes" "yes" "no" "no"
C
#[1] "yes" "yes" "no" "no" "0"
我在堆栈溢出时的第二个问题,欢迎所有提示:)
对于临床研究,我必须重新编码许多二分基线特征,其中包含“是”和“否”的多种变体。
目前我正在一个一个地重新编码这些变量,但是它需要很多行代码并且所有不同变量之间的变化非常相似。在未知或 NA 的情况下,我想重新编码为 0。
例子
library(dplyr)
A <- c("Yes", "y", "no", "n", "UK")
B <- c("yes", "Yes", "y", "no", "no")
C <- c("Y", "y", "n", "no", "uk")
#attempt 1 was to recode all variables one by one
A <- recode(A, "Yes" = "yes", "y" = "yes", "n" = "no", "UK" = "no")
B <- recode (B, "Yes" = "yes", "y" = "yes")
C <- recode(C, "Y" = "yes", "y" = "yes", "n" = "no", "uk" = "no")
#attempt 2 was to use a list option on all vectors.
levels(A) <- list("yes"=c("Likely", "y", "Y", "Yes", "yes"), "no" = c("", "No", "UK", "no", "N", "n"))
我想知道是否有一种方法可以在包含所有 A、B、C 的 list/vector 上执行此列表选项?或者也许有另一种方法可以更轻松、更有效地重新编码这些变量?
任何帮助都会很棒 :)
如果向量长度相同,您可以将它们放入数据框中,或者如果它们长度不同,则将它们放入列表中,然后使用 lapply
对所有向量应用相同的函数。您可以使用 forcats::fct_collapse
将多个级别合并为一个级别。
list_vec <- list(A, B, C)
list_vec <- lapply(list_vec, function(x) forcats::fct_collapse(x,
"yes"=c("Likely", "y", "Y", "Yes", "yes"),
"no" = c("", "No", "UK", "no", "N", "n", "uk")))
您可以从向量中使用 grepl
到 select yes
或 no
。
c("0","no","yes")[1 + grepl("^no?", A, TRUE) + 2*grepl("^ye?s?", A, TRUE)]
#[1] "yes" "yes" "no" "no" "0"
要对许多向量进行此操作,您可以使用如下循环:
for(x in c("A","B","C")) {
assign(x, c("0","no","yes")[1 + grepl("^no?", get(x), TRUE) +
2*grepl("^ye?s?", get(x), TRUE)])
}
A
#[1] "yes" "yes" "no" "no" "0"
B
#[1] "yes" "yes" "yes" "no" "no"
C
#[1] "yes" "yes" "no" "no" "0"