Reputation: 561
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?
Upvotes: 3
Views: 5573
Reputation: 887108
You could try match
df$colX <- labels[,2][match(df$colX, labels[,1])]
Or even the below should work
labels[,2][df$colX]
#[1] "LABEL3" "LABEL5" "LABEL1" "LABEL6" "LABEL1" "LABEL6" "LABEL4" "LABEL3"
#[9] "LABEL1" "LABEL2" "LABEL2" "LABEL3" "LABEL6" "LABEL4" "LABEL5" "LABEL1"
#[17] "LABEL4" "LABEL5" "LABEL3" "LABEL5" "LABEL1" "LABEL3" "LABEL1" "LABEL1"
#[25] "LABEL2"
labels <- structure(list(V1 = 1:6, V2 = c("LABEL1", "LABEL2", "LABEL3",
"LABEL4", "LABEL5", "LABEL6")), .Names = c("V1", "V2"), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6"))
set.seed(25)
df <- data.frame(colX= sample(1:6,25, replace=TRUE), colY=rnorm(25))
Upvotes: 4