Skip to content

Instantly share code, notes, and snippets.

@markdly
Last active August 2, 2018 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markdly/b9f4a3367c9a13fb6c3e71f1353788cc to your computer and use it in GitHub Desktop.
Save markdly/b9f4a3367c9a13fb6c3e71f1353788cc to your computer and use it in GitHub Desktop.
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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment