Working with labelled data

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
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)

rio::gather_attrs() converts attributes to the data.frame level

g <- rio::gather_attrs(restored_data)
str(g)
attr(g, "labels")
unlink("mydata.sav")


Try the rio package in your browser

Any scripts or data that you put into this service are public.

rio documentation built on June 22, 2024, 9:48 a.m.