I have a vector called "nationality" that indicates respondents' nationality in my data frame (df). The problem, however, is that it is currently an integer vector that goes from 1 to 193. I have another row vector called "labels" with the labels of each nationality (i.e. the first column says "Afghan", the second "Albanian", etc.). What I want to do is transforming "nationality" vector into a factor and replacing its numeric values with labels. I tried this:
df$nationality <- as.factor(df$nationality)
labels2 <- names(labels)
levels(df$nationality) <- labels2
But it does not work :(
Help, please. Thanks in advance!