如何使用 mutate_at 和 set_value_labels 来更改多个变量的值标签?
How do I use mutate_at with set_value_labels to change the value labels of multiple variables?
如何在通过 haven
从 SPSS 导入的数据框中设置多个变量的值标签。这会产生 haven_labelled
class 的变量。我正在尝试使用 [labelled][1]
包设置值标签。
#Fake data
var1<-labelled(sample(seq(1,4,1)), labels=NULL)
var2<-labelled(sample(seq(1,4,1)), labels=NULL)
var3<-labelled(sample(seq(1,4,1)), labels=NULL)
#Provide one variable to make sure I can use the vars() function so I can select out a subset of variables to mutate
out<-labelled(sample(seq(1,4,1)), labels=NULL)
df<-data.frame(var1, var2, var3, out)
#Check that these are haven_labelled as my data are
str(df)
df %>%
mutate_at(vars(starts_with('var')),set_value_labels,
"Catholic=1",
"Protestant=2",
"None=3",
"Other=4") %>%
val_labels()
最后,我希望这四个变量中的每一个都具有相同的值标签。
这行得通,但我正在尝试简化代码。
val_labels(df[,c('var1', 'var2', 'var3')])<-c(Catholic=1, Protestant=2, None=3, Other=4)
df
str(df)
as_factor(df)
只需使用 haven
而不是 labelled
这应该可行——你已经非常接近了,但你只需要将标签作为命名向量传递:
library(dplyr)
library(haven)
library(purrr)
df %>%
mutate_at(vars(starts_with('var')), ~labelled_spss(., labels = c("Catholic"=1, "Protestant"=2, "None"=3, "Other"=4))) %>%
map(print_labels)
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
$var1
<Labelled SPSS double>
[1] 1 3 4 2
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
$var2
<Labelled SPSS double>
[1] 4 3 1 2
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
$var3
<Labelled SPSS double>
[1] 3 4 2 1
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
$out
<Labelled double>
[1] 3 1 4 2
如何在通过 haven
从 SPSS 导入的数据框中设置多个变量的值标签。这会产生 haven_labelled
class 的变量。我正在尝试使用 [labelled][1]
包设置值标签。
#Fake data
var1<-labelled(sample(seq(1,4,1)), labels=NULL)
var2<-labelled(sample(seq(1,4,1)), labels=NULL)
var3<-labelled(sample(seq(1,4,1)), labels=NULL)
#Provide one variable to make sure I can use the vars() function so I can select out a subset of variables to mutate
out<-labelled(sample(seq(1,4,1)), labels=NULL)
df<-data.frame(var1, var2, var3, out)
#Check that these are haven_labelled as my data are
str(df)
df %>%
mutate_at(vars(starts_with('var')),set_value_labels,
"Catholic=1",
"Protestant=2",
"None=3",
"Other=4") %>%
val_labels()
最后,我希望这四个变量中的每一个都具有相同的值标签。 这行得通,但我正在尝试简化代码。
val_labels(df[,c('var1', 'var2', 'var3')])<-c(Catholic=1, Protestant=2, None=3, Other=4)
df
str(df)
as_factor(df)
只需使用 haven
而不是 labelled
这应该可行——你已经非常接近了,但你只需要将标签作为命名向量传递:
library(dplyr)
library(haven)
library(purrr)
df %>%
mutate_at(vars(starts_with('var')), ~labelled_spss(., labels = c("Catholic"=1, "Protestant"=2, "None"=3, "Other"=4))) %>%
map(print_labels)
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
$var1
<Labelled SPSS double>
[1] 1 3 4 2
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
$var2
<Labelled SPSS double>
[1] 4 3 1 2
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
$var3
<Labelled SPSS double>
[1] 3 4 2 1
Labels:
value label
1 Catholic
2 Protestant
3 None
4 Other
$out
<Labelled double>
[1] 3 1 4 2