简体   繁体   English

替换 R 中 2 个数据帧之间的行

[英]Replacing rows between 2 dataframes in R

I have 2 data frames, and I want to write a code that will allow me to check if a row from data frame1 exist in data frame2, and if so then I want to replace the row(s) from data frame1 with the row(s) from data frame2.我有 2 个数据帧,我想编写一个代码,让我检查数据帧 1 中的一行是否存在于数据帧 2 中,如果存在,那么我想用行替换数据帧 1 中的行( s) 来自数据框 2。 Here is an example:下面是一个例子:

dataframe1:数据框1:

name名称 A一种 B
AA机管局 1 1 1 1
BB BB 1 1 0 0
CC抄送 0 0 1 1

dataframe2:数据框2:

name名称 A一种 B
AA机管局 1 1 2 2
DD DD 1 1 3 3
EE EE 4 4 1 1

I want to switch rows between both dataframes, and the outcome will be:我想在两个数据帧之间切换行,结果将是:

dataframe1:数据框1:

name名称 A一种 B
AA机管局 1 1 2 2
BB BB 1 1 0 0
CC抄送 0 0 1 1

To clarify, I want to row AA from dataframe1 to be switched by the row AA dataframe2.澄清一下,我想从 dataframe1 行 AA 由行 AA dataframe2 切换。

This is what I tried to do:这就是我试图做的:

df1[which(df1$name %in% df2$name)[1:nrow(df2)],] <- df2

And:和:

df1$name[match(df2$name,df1$name)] <- df2$name

Both didn't work unfortunately.不幸的是,两者都没有奏效。

Thanks for helping!感谢您的帮助!

Does this work:这是否有效:

df1
  name A B
1   AA 1 1
2   BB 1 0
3   CC 0 1
df2
  name A B
1   AA 1 2
2   DD 1 3
3   EE 4 1
df2$name %in% df1$name
[1]  TRUE FALSE FALSE
df1[df1$name %in% df2$name, ] = df2[df2$name %in% df1$name, ]
df1
  name A B
1   AA 1 2
2   BB 1 0
3   CC 0 1

The natural_join is the function you are looking for natural_join是您正在寻找的功能

library(rqdatatable)

dataframe1 <- data.frame(
        name = c('AA', 'BB', 'CC'),
        A = c(1,1,0),
        B = c(1,0,1)
    )

dataframe2 <- data.frame(
    name = c('AA', 'DD', 'EE'),
    A = c(1,1,4),
    B = c(2,3,1)
)

natural_join(dataframe2, dataframe1, by = "name",
             jointype = 'RIGHT')

You can make an update join :您可以进行更新加入

i <- match(df1$name, df2$name)
j <- which(!is.na(i))
df1[j,] <- df2[i[j],]

df1
#  name A B
#1   AA 1 2
#2   BB 1 0
#3   CC 0 1

Data:数据:

df1 <- data.frame(name = c("AA","BB","CC"), A = c(1,1,0), B = c(1,0,1))
df2 <- data.frame(name = c("AA","DD","EE"), A = c(1,1,4), B = c(2,3,1))

A dplyr way using across , left_join and coalesce使用A dplyr方式acrossleft_joincoalesce

library(dplyr, warn.conflicts = F)

df1 <- data.frame(name = c("AA","BB","CC"), A = c(1,1,0), B = c(1,0,1))
df2 <- data.frame(name = c("AA","DD","EE"), A = c(1,1,4), B = c(2,3,1))

df1 %>% left_join(df2, by = 'name') %>%
  mutate(across(ends_with('.y'), ~coalesce(., get(gsub('\\.y', '\\.x', cur_column()))),
                .names = "{gsub('.y', '', .col)}"), .keep = 'unused')

#>   name A B
#> 1   AA 1 2
#> 2   BB 1 0
#> 3   CC 0 1

Created on 2021-07-06 by the reprex package (v2.0.0)reprex 包( v2.0.0 ) 于 2021 年 7 月 6 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM