Last active
August 29, 2015 14:22
-
-
Save nickbloom/decdedc75268996b7621 to your computer and use it in GitHub Desktop.
Load a DTA file, without labels
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install and Load Packages | |
install.packages('haven') | |
library('haven') | |
install.packages('stringr') | |
library('stringr') | |
# Record the function | |
remove_labels <- function (x){ | |
for (i in names(x)) { | |
v <- x[[i]] | |
attr(v, "label") <- NULL | |
attr(v, "labels") <- NULL | |
is_it_labelled <- class(v) == "labelled" | |
if (is_it_labelled) { | |
is_it_character <- TRUE %in% str_detect(v, "[a-zA-Z]") | |
if (is_it_character) { | |
class(v) <- NULL | |
class(v) <- "character" | |
} | |
else { | |
class(v) <- NULL | |
class(v) <- "numeric" | |
} | |
} | |
x[[i]] <- v | |
} | |
data.frame(x) | |
} | |
# To load a .dta dataset, for example: | |
my_data_frame <- read_dta('path/to/your/file.DTA') | |
my_data_frame <- remove_labels(my_data_frame) | |
names(my_data_frame) <- set_names(tolower(names(my_data_frame))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment