Search code examples
rmergebind

Manipulate Vector and Data Frame in R


list1=c(1,6,3,4,4,5)
data=data.frame("colA" = c(1:6),
"colB"=c(4,3,1,8,9,8))

I have 'list1' and 'data'

I wish to match the values in 'colB' to the ones in list1 using 'colA' as a key aso it looks like

enter image description here


Solution

  • Perhaps, we need match

    data.frame(list1, colB = data$colB[match(list1, data$colA)])
    #  list1 colB
    #1     1    4
    #2     6    8
    #3     3    1
    #4     4    8
    #5     4    8
    #6     5    9