假设我有以下由
tibble
和 haven
包制作的 tibble:
library(tibble)
library(haven)
# Create numerical values
values <- c(1:5)
# Combine values and colors into a named vector
color_choices <- setNames(values, c("Don't know", "Red", "Blue", "Green", "Yellow"))
name_choices <- setNames(values, c("Don't know", "John", "Paul", "Ringo", "George"))
# Create a tibble with the labelled column
data <- tibble(respondent_ID = seq(1:10),
colour_choice = labelled(sample(1:5, 10, replace = TRUE), labels = color_choices),
name_choice = labelled(sample(1:5, 10, replace = TRUE), labels = name_choices))
data
现在我想更改一些变量的避风港标签。具体来说,我只想将
value = 1
的标签从 "Don't know"
更改为 "Not sure"
,但我想跨多个变量执行此操作。
这实现了我想要的
colour_choice
变量的结果:
data_replaced <- data
color_choices2 <- color_choices
names(color_choices2)[1] <- "Not sure"
val_labels(data_replaced$colour_choice) <- color_choices2
但是,这很乏味,原因有两个:首先,即使对于一个变量,它也是低效的,因为当只有一个需要替换时,它涉及为变量中的所有标签创建一个新的命名向量。 [
val_labels(data_replaced$colour_choice) <- c("Not sure" = 1)
导致其他标签被删除]。其次,它绝对不可扩展。
我一直在使用
memisc::relabel
函数尝试 dplyr 方法(无论如何这都是更好的方法),但一直碰壁,想知道是否有人可以提出建议?这是我现在的位置:
data_replaced <- data %>%
mutate_at(vars(colour_choice, name_choice), ~ memisc::relabel(., "Don't know" = "Not sure"))
您可以使用
labelled:set_value()
创建辅助函数,然后在 mutate(across())
中调用它:
library(dplyr)
library(labelled)
change_value_label <- function(x, value, new_label) {
val_label(x, value) <- new_label
x
}
rm(x)
data %>%
mutate(across(
colour_choice:name_choice,
\(x) change_value_label(x, 1, "Not sure")
))
# # A tibble: 10 × 3
# respondent_ID colour_choice name_choice
# <int> <int+lbl> <int+lbl>
# 1 1 5 [Yellow] 5 [George]
# 2 2 4 [Green] 3 [Paul]
# 3 3 5 [Yellow] 5 [George]
# 4 4 1 [Not sure] 3 [Paul]
# 5 5 2 [Red] 1 [Not sure]
# 6 6 3 [Blue] 5 [George]
# 7 7 5 [Yellow] 2 [John]
# 8 8 2 [Red] 1 [Not sure]
# 9 9 1 [Not sure] 5 [George]
# 10 10 2 [Red] 5 [George]