Formats SAS, SPSS, and Stata use haven
as import and
export functions. And these formats can have the so-called labelled
data. For more information, please read
vignette("semantics", "haven")
. Here, we provide a quick
guide on how to work with labelled data using rio
.
You can use haven::labelled()
to create labelled
data.
Or directly using attrs
mydata <- data.frame(gender, rating)
Round trip: The data labels are retained. But they are at the variable level.
export(mydata, "mydata.sav")
restored_data <- rio::import("mydata.sav")
str(restored_data)
#> 'data.frame': 5 obs. of 2 variables:
#> $ gender: chr "M" "F" "F" "F" ...
#> ..- attr(*, "format.spss")= chr "A1"
#> ..- attr(*, "labels")= Named chr [1:2] "M" "F"
#> .. ..- attr(*, "names")= chr [1:2] "Male" "Female"
#> $ rating: num 5 3 4 1 2
#> ..- attr(*, "format.spss")= chr "F8.0"
#> ..- attr(*, "labels")= Named num [1:2] 1 5
#> .. ..- attr(*, "names")= chr [1:2] "Good" "Bad"
rio::gather_attrs()
converts attributes to the
data.frame level
g <- rio::gather_attrs(restored_data)
str(g)
#> 'data.frame': 5 obs. of 2 variables:
#> $ gender: chr "M" "F" "F" "F" ...
#> ..- attr(*, "labels")= Named chr [1:2] "M" "F"
#> .. ..- attr(*, "names")= chr [1:2] "Male" "Female"
#> $ rating: num 5 3 4 1 2
#> ..- attr(*, "labels")= Named num [1:2] 1 5
#> .. ..- attr(*, "names")= chr [1:2] "Good" "Bad"
#> - attr(*, "format.spss")=List of 2
#> ..$ gender: chr "A1"
#> ..$ rating: chr "F8.0"
#> - attr(*, "labels")=List of 2
#> ..$ gender: Named chr [1:2] "M" "F"
#> .. ..- attr(*, "names")= chr [1:2] "Male" "Female"
#> ..$ rating: Named num [1:2] 1 5
#> .. ..- attr(*, "names")= chr [1:2] "Good" "Bad"
attr(g, "labels")
#> $gender
#> Male Female
#> "M" "F"
#>
#> $rating
#> Good Bad
#> 1 5