tfr
tfr

Reputation: 71

R: read .dta file and use value labels only for selected variables to create a factor

What is the easiest way to read a .dta file in R and convert only specific variables as factors, using Stata value labels? I didn't find a way to specify the convert.factors option in the foreign package. I also failed with the mimisc package.

library('foreign')
df <- read.dta("statafile.dta", convert.factors = TRUE)

Upvotes: 2

Views: 1427

Answers (1)

lebatsnok
lebatsnok

Reputation: 6459

I'd suggest something like this:

df <- read.dta("statafile.dta", convert.factors = FALSE)
df2 <- read.dta("statafile.dta", convert.factors = TRUE)
cols2convert <- c(3,7,9,11,36)  # columns for which you want convert.factors 2B true
df[,cols2convert] <- df2[,cols2convert]
rm(df2)

Upvotes: 2

Related Questions