简体   繁体   English

如何用所有列的另一个数据框中的匹配行替换一个数据框中的行

[英]How to replace row in one data frame with matching rows from another dataframe for all columns

I would like to replace a full row of data in one dataframe with matching rows from another dataframe.我想用另一个数据框中的匹配行替换一个数据框中的一整行数据。 I have a reproducible example with only a couple of columns, but in practice I have a dataframe with dozens of columns.我有一个只有几列的可重现示例,但实际上我有一个包含数十列的数据框。

# main dataframe
df1 <- tibble(id = letters[1:5], v1 = seq(1,5), v2 = seq(1,5), v3 = seq(1,5))
>df1
# A tibble: 5 x 4
  id       v1    v2    v3
  <chr> <int> <int> <int>
1 a         1     1     1
2 b         2     2     2
3 c         3     3     3
4 d         4     4     4
5 e         5     5     5
# values to replace
df2 <- tibble(id = letters[3:4], v1 = rep(0,2), v2 = rep(0,2), v3 = rep(0,2))
> df2
# A tibble: 2 x 4
  id       v1    v2    v3
  <chr> <dbl> <dbl> <dbl>
1 c         0     0     0
2 d         0     0     0
# what the final result should look like
result <- tibble(id = c("a", "b", "c", "d", "e"), v1 = c(1, 2, 0, 0, 5), v2 = c(1, 2, 0, 0, 5), v3 = c(1, 2, 0, 0, 5))
>result
# A tibble: 5 x 4
  id       v1    v2    v3
  <chr> <dbl> <dbl> <dbl>
1 a         1     1     1
2 b         2     2     2
3 c         0     0     0
4 d         0     0     0
5 e         5     5     5

Here is one solution using tidyverse这是使用tidyverse一种解决方案

library(tidyverse)
df1 %>%
  #Stay with the rows that are not found in df2 according to its id
  filter(! id %in% df2$id) %>%
  #bind rows of df2
  bind_rows(df2) %>%
  #Order data according to id values
  arrange(id)

根据您的评论,如果您的 ID 存在于 df2 但不在 df1 中,您可以执行以下操作:

df1[na.omit(match(df2$id, df1$id)),] <- df2[df2$id %in% df1$id,]

This is a straightforward solution这是一个简单的解决方案

df3 <- left_join(df1, df2, by = "id", suffix = c("", ".x"))
df3[!is.na(df3$v1.x), 2:4] <- df3[!is.na(df3$v1.x), 5:7]
df3[, 5:7] <- NULL

暂无
暂无

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

相关问题 如何通过与 R 中的另一个数据框匹配来选择矩阵中的所有行和列? - How can I select all rows and columns from a matrix by matching with another data frame in R? 将一个数据框中的列匹配到另一个 - Matching columns from one data frame to another 将数据框中的一行添加到另一数据框中的所有行 - Add one row in a data frame to all rows of another data frame 将选定列中的值和一个数据框中的匹配行覆盖到另一个数据框中,R - Overwrite values from selected columns and matching rows from one data frame into another, R R-根据行匹配,使用来自另一个数据框的值填充一个数据框 - R - Populate one data frame with values from another dataframe, based on row matching 用另一个数据框中匹配行中的值替换丢失的数据 - Replace missing data with values from matching rows in another dataframe 使用另一数据框的一行中的值替换一个数据框的一列中的所有值(按行名和列名匹配) - Replace all values in a column of one dataframe using values in a row of another dataframe (matching by row name and column name) 将一个数据帧中的行与第二秒中的列匹配 - Matching rows in one data frame with columns in a second 如何从另一个有多个匹配行的数据框中添加列 - How to add columns from another data frame where there are multible matching rows 根据行号将列/行从一个数据帧映射到另一个数据帧 - Mapping columns/rows from one dataframe to another based on row number
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM