替换匹配项中的值 [英] Replacing values in a column where there is a match

查看:67
本文介绍了替换匹配项中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是R编程的新手,并且坚持下面的示例.

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

基本上我有两个数据集:

Basically I have two data sets:

数据集1:

ID       Category        
1        CatZZ         
2        CatVV         
3        CatAA  
4        CatQQ

数据集2:

ID  Category  
1   Cat600  
3   Cat611 

我正在尝试用数据集2中的类别"值覆盖数据集1中的类别"值,其中两个数据集之间存在ID匹配.

I'm trying to overwrite the 'category' values in dataset1 with the 'category' values in dataset2 where there is an ID match between the two data sets.

所以结果看起来像这样:

So the outcome would look something like this:

数据集1:

ID  Category    
1   Cat600  
2   CatVV  
3   Cat611  
4   CatQQ  

推荐答案

tidyverse中,您可以执行以下操作:

In tidyverse you can do:

df1 %>%
 left_join(df2, by = c("ID" = "ID")) %>% #Merging the two dfs on ID
 mutate(Category = if_else(!is.na(Category.y), Category.y, Category.x)) %>% #If there is a match, taking the value from df2, otherwise from df1
 select(ID, Category) #Deleting the redundant variables

  ID Category
1  1   Cat600
2  2    CatVV
3  3   Cat611
4  4    CatQQ

或者:

df1 %>%
 left_join(df2, by = c("ID" = "ID")) %>% #Merging the two dfs on ID
 gather(var, val, -ID) %>% #Transforming the data from wide to long format
 arrange(ID) %>% #Arranging by ID
 group_by(ID) %>% #Grouping by ID
 mutate(Category = if_else(!is.na(nth(val, 2)), nth(val, 2), first(val))) %>% #If non-NA, taking the value from df2, otherwise from df1
 spread(var, val) %>% #Returning the data to wide format
 select(ID, Category) #Removing the redundant variables 

     ID Category
  <int> <chr>   
1     1 Cat600  
2     2 CatVV   
3     3 Cat611  
4     4 CatQQ

样本数据:

df1 <- read.table(text = "ID       Category        
1        CatZZ         
2        CatVV         
3        CatAA  
4        CatQQ", header = TRUE, stringsAsFactors = FALSE)

df2 <- read.table(text = "ID  Category  
1   Cat600  
                  3   Cat611", header = TRUE, stringsAsFactors = FALSE)

这篇关于替换匹配项中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆