如何用 R 中另一个小标题的插补列替换小标题中的 NA 列

问题描述

我想使用 NA 中的估算值将 df 中的列替换为 df2 以获得 df3。 我可以用 left_joincoalesce 做到这一点,但我认为这种方法不能很好地概括。有没有更好的办法?

library(tidyverse)

df <- tibble(c = c("a","a","b","b"),d = c(1,2,3,1,3),x = c(1,NA,4,5,6),y = c(1,z = c(1,7,6))

# I want to replace NA in df by df2

df2 <- tibble(c = c("a","a"),2))

# to get

df3 <- tibble(c = c("a",6))

# is there a better solution than coalesce?

df3 <- df %>% left_join(df2,by = c("c","d")) %>%
  mutate(x = coalesce(x.x,x.y),y = coalesce(y.x,y.y)) %>%
  select(-x.x,-x.y,-y.x,-y.y)
Created on 2021-06-17 by the reprex package (v2.0.0)

解决方法

这是一个自定义函数,用于合并 all .x.y 列,可选择重命名和删除列。

#' Coalesce all columns duplicated in a previous join.
#'
#' Find all columns resulting from duplicate names after a join
#' operation (e.g.,`dplyr::*_join` or `base::merge`),then coalesce
#' them pairwise.
#'
#' @param x data.frame
#' @param suffix character,length 2,the same string suffixes
#'   appended to column names of duplicate columns; should be the same
#'   as provided to `dplyr::*_join(.,suffix=)` or `base::merge(.,#'   suffixes=)`
#' @param clean logical,whether to remove the suffixes from the LHS
#'   columns and remove the columns on the RHS columns
#' @param strict logical,whether to enforce same-classes in the LHS
#'   (".x") and RHS (".y") columns; while it is safer to set this to
#'   true (default),sometimes the conversion of classes might be
#'   acceptable,for instance,if one '.x' column is 'numeric' and its
#'   corresponding '.y' column is 'integer',then relaxing the class
#'   requirement might be acceptable
#' @return 'x',coalesced,optionally cleaned
#' @export
coalesce_all <- function(x,suffix = c(".x",".y"),clean = FALSE,strict = TRUE) {
  nms <- colnames(x)
  Xs <- endsWith(nms,suffix[1])
  Ys <- endsWith(nms,suffix[2])
  # x[Xs] <- Map(dplyr::coalesce,x[Xs],x[Ys])
  # x[Xs] <- Map(data.table::fcoalesce,x[Ys])
  x[Xs] <- Map(function(dotx,doty) {
    if (strict) stopifnot(identical(class(dotx),class(doty)))
    isna <- is.na(dotx)
    replace(dotx,isna,doty[isna])
  },x[Ys])
  if (clean) {
    names(x)[Xs] <- gsub(glob2rx(paste0("*",suffix[1]),trim.head = TRUE),"",nms[Xs])
    x[Ys] <- NULL
  }
  x
}

在行动:

df %>%
  left_join(df2,by = c("c","d")) %>%
  coalesce_all()
# # A tibble: 6 x 7
#   c         d   x.x   y.x     z   x.y   y.y
#   <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
# 1 a         1     1     1     1     1     1
# 2 a         2     2     2     2     2     2
# 3 a         3     3     2     7     3     2
# 4 b         1     4     4     4    NA    NA
# 5 b         2     5     5     5    NA    NA
# 6 b         3     6     6     6    NA    NA

df %>%
  left_join(df2,"d")) %>%
  coalesce_all(clean = TRUE)
# # A tibble: 6 x 5
#   c         d     x     y     z
#   <chr> <dbl> <dbl> <dbl> <dbl>
# 1 a         1     1     1     1
# 2 a         2     2     2     2
# 3 a         3     3     2     7
# 4 b         1     4     4     4
# 5 b         2     5     5     5
# 6 b         3     6     6     6

我在 Map 中包含了两个合并函数作为 base-R 的替代。一个优点是 strict 参数:dplyr::coalesce 将默默地允许 integernumeric 合并,而 data.table::fcoalesce 不会。如果这是可取的,请使用您喜欢的。 (另一个优点是两个非基础合并函数都接受任意数量的要合并的列,这在本实现中不是必需的。)

,

您可以通过使用 across 和使用 .names & .keep 参数一次改变所有列,如下所示

library(dplyr,warn.conflicts = F)

df %>% left_join(df2,"d")) %>%
  mutate(across(ends_with('.x'),~ coalesce(.,get(gsub('.x','.y',cur_column()))),.names = '{gsub(".x$",.col)}'),.keep = 'unused')
#> # A tibble: 6 x 5
#>   c         d     z     x     y
#>   <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 a         1     1     1     1
#> 2 a         2     2     2     2
#> 3 a         3     7     3     2
#> 4 b         1     4     4     4
#> 5 b         2     5     5     5
#> 6 b         3     6     6     6

reprex package (v2.0.0) 于 2021 年 6 月 17 日创建

,

我尝试了另一种方法,过滤 c,使用 df 删除 NA 的所有列,使用 df2 加入并将未过滤的 df 的行与df3

df3 <- df %>% filter(c == "a") %>% select_if(~ !any(is.na(.))) %>%
  left_join(df2,"d"))
df3 <- bind_rows(df %>% filter(!c == "a"),df3) %>% arrange(c,d)
df3
#> # A tibble: 6 x 5
#>   c         d     x     y     z
#>   <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 a         1     1     1     1
#> 2 a         2     2     2     2
#> 3 a         3     3     2     7
#> 4 b         1     4     4     4
#> 5 b         2     5     5     5
#> 6 b         3     6     6     6
Created on 2021-06-17 by the reprex package (v2.0.0)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...