library(dplyr)
library(haven)
path <- system.file("examples", "iris.sav", package = "haven")
df <- read_sav(path)
head(df, 3)
#> # A tibble: 3 x 5
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> <dbl> <dbl> <dbl> <dbl> <dbl+lbl>
#> 1 5.1 3.5 1.4 0.2 1
#> 2 4.9 3 1.4 0.2 1
#> 3 4.7 3.2 1.3 0.2 1
typeof(df$Species)
#> [1] "double"
attributes(df$Species)
#> $format.spss
#> [1] "F8.0"
#>
#> $class
#> [1] "labelled"
#>
#> $labels
#> setosa versicolor virginica
#> 1 2 3
df %>%
select(Species) %>%
mutate(Species_as_factor = as_factor(Species)) %>%
mutate(Species_no_labels = zap_labels(Species)) %>%
head(3)
#> # A tibble: 3 x 3
#> Species Species_as_factor Species_no_labels
#> <dbl+lbl> <fct> <dbl>
#> 1 1 setosa 1
#> 2 1 setosa 1
#> 3 1 setosa 1
Created on 2018-08-02 by the reprex package (v0.2.0).