Replace column values based on column in another dataframe

こ雲淡風輕ζ 提交于 2021-01-29 18:47:38

问题


I would like to replace some column values in a df based on column in another data frame This is the head of the first df:

 df1
A tibble: 253 x 2
      id sum_correct
    <int>       <dbl>
 1 866093          77
 2 866097          95
 3 866101          37
 4 866102          65
 5 866103          16
 6 866104          72
 7 866105          99
 8 866106          90
 9 866108          74
10 866109          92

and some sum_correct need to be replaced by the correct values in another df using the id to trigger the replacement

df 2 
A tibble: 14 x  2
     id sum_correct
    <int>       <dbl>
 1 866103          61
 2 866124          79
 3 866152          85
 4 867101          24
 5 867140          76
 6 867146          51
 7 867152          56
 8 867200          50
 9 867209          97
10 879657          56
11 879680          61
12 879683          58
13 879693          77
14 881451          57

how I can achieve this in R studio? thanks for the help in advance.


回答1:


you can do a left_join and then use coalesce:

library(dplyr)
left_join(df1, df2, by = "id", suffix = c("_1", "_2")) %>%
  mutate(sum_correct_final = coalesce(sum_correct_2, sum_correct_1))

The new column sum_correct_final contains the value from df2 if it exists and from df1 if a corresponding entry from df2 does not exist.




回答2:


You can make an update join using match to find where id matches and remove non matches (NA) with which:

idx <- match(df1$id, df2$id)
idxn <- which(!is.na(idx))
df1$sum_correct[idxn]  <- df2$sum_correct[idx[idxn]]
df1
       id sum_correct
1  866093          77
2  866097          95
3  866101          37
4  866102          65
5  866103          61
6  866104          72
7  866105          99
8  866106          90
9  866108          74
10 866109          92


来源:https://stackoverflow.com/questions/59134813/replace-column-values-based-on-column-in-another-dataframe

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!