My situation is that I have a data frame with a column filled with the integers 1 to 6. I would like to replace these integers with more descriptive labels, provided in another data frame which acts as a "key":
V1 V2
1 1 LABEL1
2 2 LABEL2
3 3 LABEL3
4 4 LABEL4
5 5 LABEL5
6 6 LABEL6
So whenever I find a number 1 in the first data frame column (df$colX), I want to replace it with LABEL1 (i.e., label column 2, where df$colX == label column 1).
I have tried
replace(df$colX,labels[,1],labels[,2])
but this just turns the integers into quoted integers for some reason.
I could do this with a for loop, but that seems very slow.
I have also followed some advice on StackOverflow about factors, but none of the columns I'm working with here seem to involve factors (read with stringsAsFactors = FALSE). Any ideas?
merge
for this. – Roland