Adjust STATA objects to R format
check_stata_format.RdWhen 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.
Argumentos
- obj
A dataframe or a vector
- return_labels
Logical. Return variable values as integers or with its labels. Default TRUE (labels).
- new_class
When
return_lavelsset to FALSE, The desired class of the vector/variables. Choices: 'numeric', 'character' or 'logical'. By default it is converted to numeric.
Detalles
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.
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.
Ejemplos
# Import data
ecv2020 <- haven::read.dta('ecv2020.dta')
#> Error in loadNamespace(x): there is no package called ‘haven’
# The function detects which columns must be changed
ecv2020 <- check_stata_format(ecv2020)
#> Error in is.data.frame(obj): objeto 'ecv2020' no encontrado
# It can be applied to one variable
ecv2020$situacion_hogar <- check_stata_format( ecv2020$situacion_hogar )
#> Error in is.data.frame(obj): objeto 'ecv2020' no encontrado
ecv2020 <- ecv2020 %>%
mutate( situacion_hogar = check_stata_format( situacion_hogar ) )
#> Error in mutate(., situacion_hogar = check_stata_format(situacion_hogar)): no se pudo encontrar la función "mutate"