Replace values in data frame based on other data frame in R

后端 未结 4 2239
说谎
说谎 2020-12-06 05:21

In the below example, userids is my reference data frame and userdata is the data frame where the replacements should take place.

&         


        
4条回答
  •  时光说笑
    2020-12-06 05:50

    Here's a possible solution, which will also work on datasets with multiple records of each ID, though we will need to coerce the ID and FRIENDID variables to character first:

    > userdata$ID <- sapply(userdata$ID, function(x){gsub(x, userids[userids$USER==x, 2], x)})
    > userdata$FRIENDID <- sapply(userdata$FRIENDID, function(x){gsub(x, userids[userids$USER==x, 2], x)})
    

提交回复
热议问题