Reputation: 509
I have a Stata dataset (.dta file) that contains one variable, RGA (this is a MWE, I actually have tons of variables). This variable takes 3 factor values: 1, 2 and 3. These factors refer to meaningful things (so-called "value labels") and the association between the factors and their value labels is in a separate .txt Stata-like file, fully reproduced here:
. label define RGA_l
1 "meaning of 1"
2 "meaning of 2"
3 "meaning of 3"
. label values RGA RGA_l
I load my .dta file into R through the haven
package. I would like to have an easy access to the value labels of RGA within R, notably to be able to quickly match RGA's values with their value label to produce readable output. How can I read this separate .txt file into R in a way that I can match it with my dataset?
Upvotes: 0
Views: 987
Reputation: 15072
I don't know exactly what type of column haven
imported (try using str()
on your dataframe) but here is how you create factors in R. The factor
function is somewhat confusing because factors don't actually have labels per se, they only have levels, but the argument is still called labels
.
set.seed(100)
df <- data.frame(RGA_1 = sample.int(3, 10, replace = TRUE))
df$RGA_1 <- factor(df$RGA_1, labels = c("meaning1", "meaning2", "meaning3"))
df
#> RGA_1
#> 1 meaning1
#> 2 meaning1
#> 3 meaning2
#> 4 meaning1
#> 5 meaning2
#> 6 meaning2
#> 7 meaning3
#> 8 meaning2
#> 9 meaning2
#> 10 meaning1
Created on 2018-05-30 by the reprex package (v0.2.0).
Upvotes: 0