Matching the values ​​of two vectors

I have a data frame:

AA<-c(4,1,5,3,2,13,17,16,14,15,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA)

BB<-c(3,15,4,2,13,19,20,14,7,12,8,18,11,5,16,17,9,6,10,1)

CC<-c(118,106,115,120,105,111,104,101,102,110,119,108,113,109,114,116,103,107,112,117)

DD<-data.frame(AA,BB,CC)

      

How do I create a vector containing values CC

that correspond to the numbers BB

corresponding to the numbers AA

?

I want EE to look like this:, 115,117,109,118,120,105,116,114,101,106

(I don't care what happens after line 10)

+3


source to share


1 answer


You can use the match function to search for AA positions in BB and then simply select those positions from CC.



DD$CC[match(DD$AA, DD$BB)]

      

+2


source







All Articles