Skip to content

Instantly share code, notes, and snippets.

@nickbloom
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickbloom/decdedc75268996b7621 to your computer and use it in GitHub Desktop.
Save nickbloom/decdedc75268996b7621 to your computer and use it in GitHub Desktop.
Load a DTA file, without labels
# 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