The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.

Working with labelled data

library(rio)
library(haven)

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.

gender <- haven::labelled(
                     c("M", "F", "F", "F", "M"),
                     c(Male = "M", Female = "F"))

Or directly using attrs

rating <- sample(1:5)
attr(rating, "labels") <-  c(c(Good = 1, Bad = 5))
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 1 2 4
#>   ..- 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 1 2 4
#>   ..- 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

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.