View source: R/check_stata_format.r
check_stata_format | R Documentation |
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.
check_stata_format(obj)
## S3 method for class 'data.frame'
check_stata_format(obj)
## Default S3 method:
check_stata_format(obj)
obj |
A dataframe or a vector |
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.
The same object with its changed class.
# 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 ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.