check_stata_format: Adjust STATA objects to R format

View source: R/check_stata_format.r

check_stata_formatR Documentation

Adjust STATA objects to R format

Description

When importing data from Stata, some vectors take 'haven_labelled' as their class with specific attributes. Given that this class (with its attributes) does not exist in R this can lead to errors or unexpected behaviours.

Usage

check_stata_format(obj)

## S3 method for class 'data.frame'
check_stata_format(obj)

## Default S3 method:
check_stata_format(obj)

Arguments

obj

A dataframe or a vector

Details

This function is provided for changing the class of the object. If it is a dataframe is not necessary to previously select the columns, since the function detects which variables need to be modified.

The class of the new vector is guessed.

When importing .dta data, coerce functions such as as.numeric or as.character work well in principle. However, if this .dta is transformed to a .rds (for memory reasons) the new vectors are intractables for these functions. Thus, the function is designed to deal with any type of situation.

Value

The same object with its changed class.

Examples

# Import data
ecv2020 <- haven::read.dta('ecv2020.dta')
# The function detects which columns must be changed
ecv2020 <- check_stata_format(ecv2020)

# It can be applied to one variable
ecv2020$situacion_hogar <- check_stata_format( ecv2020$situacion_hogar )

ecv2020 <- ecv2020 %>%
   mutate( situacion_hogar = check_stata_format( situacion_hogar )  )

octmedina/ksnet documentation built on April 18, 2023, 3:34 p.m.