简体   繁体   中英

Replacing values in a column of a data.frame with values from another data.frame

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 (ie, 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?

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"

data

 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM