haven::read_sav showing value labels rather than c

2019-07-14 06:17发布

问题:

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"

回答1:

Found a very simple solution within haven package

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


回答2:

You may use a function called characterize() or factorize() from rio package to do the conversion of this type of data structure.

for example:

data$Species1 <- rio::characterize(data$Species)

If you choose characterize, the column is converted to character, but in case you choose to use factorize, the column is converted to factor.

You may visit here for reference:

Not sure you wanted like this or not, May be you want to convert it while importing.

Thanks, I hope this helps.

Running the conversion using factorize().

Output:

# A tibble: 150 x 6
   #    Sepal.Length Sepal.Width Petal.Length Petal.Width   Species Species1
   #           <dbl>       <dbl>        <dbl>       <dbl> <dbl+lbl>   <fctr>
   #  1          5.1         3.5          1.4         0.2         1   setosa
   #  2          4.9         3.0          1.4         0.2         1   setosa
   #  3          4.7         3.2          1.3         0.2         1   setosa
   #  4          4.6         3.1          1.5         0.2         1   setosa
   #  5          5.0         3.6          1.4         0.2         1   setosa