labelled (version 1.0.0)

to_labelled: Convert to labelled data

Description

Convert data imported with foreign or memisc to labelled data. to_labelled could also be used to convert a factor to a labelled vector.

Usage

to_labelled(x)

# S3 method for data.frame to_labelled(x)

# S3 method for list to_labelled(x)

# S3 method for data.set to_labelled(x)

# S3 method for importer to_labelled(x)

foreign_to_labelled(x)

memisc_to_labelled(x)

# S3 method for factor to_labelled(x)

Arguments

x

Data to convert to labelled data frame

Value

a tbl data frame or a labelled vector.

Details

to_labelled is a general wrapper calling the appropriate sub-functions.

memisc_to_labelled converts a data.set object created with memisc package to a labelled data frame.

foreign_to_labelled converts data imported with read.spss or read.dta from foreign package to a labelled data frame, i.e. using labelled class.

Factors will not be converted. Therefore, you should use use.value.labels = FALSE when importing with read.spss or convert.factors = FALSE when importing with read.dta.

To convert correctly defined missing values imported with read.spss, you should have used to.data.frame = FALSE and use.missings = FALSE. If you used the option to.data.frame = TRUE, meta data describing missing values will not be attached to the import. If you used use.missings = TRUE, missing values would have been converted to NA.

So far, missing values defined in Stata are always imported as NA by read.dta and could not be retrieved by foreign_to_labelled.

See Also

labelled (foreign), read.spss (foreign), read.dta (foreign), data.set (memisc), importer (memisc).

Examples

Run this code
# NOT RUN {
  # from foreign
  library(foreign)
  df <- to_labelled(read.spss(
    'file.sav',
    to.data.frame = FALSE,
    use.value.labels = FALSE,
    use.missings = FALSE
 ))
 df <- to_labelled(read.dta(
   'file.dta',
   convert.factors = FALSE
 ))

 # from memisc
 library(memisc)
 nes1948.por <- UnZip('anes/NES1948.ZIP', 'NES1948.POR', package='memisc')
 nes1948 <- spss.portable.file(nes1948.por)
 df <- to_labelled(nes1948)
 ds <- as.data.set(nes19480)
 df <- to_labelled(ds)
# }
# NOT RUN {
# }

Run the code above in your browser using DataLab