将两个数据框按单元格合并

4

我有两个数据框(df1和df2),其中一些单元格为空(NAs)。

df1<-data.frame(code=c("A","B","C","D"),
                x=c(2.3,NA,3.1,2.6),
                y=c(4.1,2,NA,8))

df2<-data.frame(code=c("A","B","C","D"),
                x=c(NA,8.1,NA,NA),
                y=c(NA,NA,0.5,NA))

我希望用df2中相应的值填充df1中的NA单元格。
期望的结果:
  code   x   y
1    A 2.3 4.1
2    B 8.1 2.0
3    C 3.1 0.5
4    D 2.6 8.0

我用for循环(扫描每个单元格)完成了它。

虽然这样可以运行,但我认为有更高效的方法...而且我喜欢学习新技巧...

提前感谢。

4个回答

2

使用 purrr::map2_dfc 的一种可能解决方案:

library(tidyverse)

map2_dfc(df1, df2, ~ if_else(is.na(.x), .y, .x))

#> # A tibble: 4 × 3
#>   code      x     y
#>   <chr> <dbl> <dbl>
#> 1 A       2.3   4.1
#> 2 B       8.1   2  
#> 3 C       3.1   0.5
#> 4 D       2.6   8

1

为了速度

# set as data.table
lapply(list(df1, df2), \(i) setDT(i))

# custom efficient coalesce
coalesce2 <- function(...)
  {
  Reduce(function(x, y) {
    i <- which(is.na(x))
    x[i] <- y[i]
    x},
    list(...))
  }

# join
df3 <- df2[df1, on =.(code)]

# apply coalesce
df3[, `:=` (x = coalesce2(i.x, x)
            , y = coalesce2(i.y, y)
            )
    ][, c('i.x', 'i.y') := NULL
      ]

自定义合并函数 [来源于](https://dev59.com/WGIk5IYBdhLWcg3wdd_Y) - Sweepy Dodo

1

一个基本的R选项

df1[is.na(df1)] = as.numeric(df2[is.na(df1)])
df1

1

使用 coalesce

library(dplyr)
do.call(coalesce, list(df1, df2))

  code   x   y
1    A 2.3 4.1
2    B 8.1 2.0
3    C 3.1 0.5
4    D 2.6 8.0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接