本文介绍了Haven :: read_sav显示值标签而不是代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用haven
将.sav
文件导入到R
.我想知道如何显示值标签而不是数字代码.在下面的示例中,我想显示物种名称,而不是数字1、2、3.
I'm using haven
to import a .sav
file into R
. I wonder how to show value labels rather than numeric codes. In the following example I want to show Species names rather than numbers 1, 2, 3.
library(haven)
path <- system.file("examples", "iris.sav", package = "haven")
df1 <- read_sav(path)
head(df1)
# A tibble: 6 x 5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<dbl> <dbl> <dbl> <dbl> <dbl+lbl>
1 5.10 3.50 1.40 0.200 1
2 4.90 3.00 1.40 0.200 1
3 4.70 3.20 1.30 0.200 1
4 4.60 3.10 1.50 0.200 1
5 5.00 3.60 1.40 0.200 1
6 5.40 3.90 1.70 0.400 1
str(df1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 150 obs. of 5 variables:
$ Sepal.Length: atomic 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
..- attr(*, "format.spss")= chr "F8.2"
$ Sepal.Width : atomic 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
..- attr(*, "format.spss")= chr "F8.2"
$ Petal.Length: atomic 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
..- attr(*, "format.spss")= chr "F8.2"
$ Petal.Width : atomic 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
..- attr(*, "format.spss")= chr "F8.2"
$ Species :Class 'labelled' atomic [1:150] 1 1 1 1 1 1 1 1 1 1 ...
.. ..- attr(*, "format.spss")= chr "F8.0"
.. ..- attr(*, "labels")= Named num [1:3] 1 2 3
.. .. ..- attr(*, "names")= chr [1:3] "setosa" "versicolor" "virginica"
推荐答案
在haven
包
haven::as_factor(df1)
# A tibble: 150 x 5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<dbl> <dbl> <dbl> <dbl> <fct>
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
7 4.6 3.4 1.4 0.3 setosa
8 5 3.4 1.5 0.2 setosa
9 4.4 2.9 1.4 0.2 setosa
10 4.9 3.1 1.5 0.1 setosa
# ... with 140 more rows
这篇关于Haven :: read_sav显示值标签而不是代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!