Replacing values in a column where there is a match

后端 未结 5 1594
南旧
南旧 2021-01-22 06:48

I\'m new to R programming and I\'m stuck on the example below.

Basically I have two data sets:

dataset1:

ID       Category        
1        CatZ         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-22 07:31

    Using a combination of base R match function and data.table's set function:

    matchinds = na.omit(match(dataset1$ID,dataset2$ID)) # this will give index of dataset2$ID where values of dataset1$ID were equal to values of dataset2$ID
    
    set(x=dataset1,i=matchinds,j="Category",value=dataset2$category[matchinds])  #this will set values at matching indexes in dataset1 Category column equal to Category column matching index values in dataset2
    

提交回复
热议问题