联接数据,但忽略缺失值

时间:2019-03-29 18:52:43

标签: r dplyr na semi-join

在使用dplyr联接数据帧时遇到了一些麻烦,我想忽略NA。

我拥有的数据很大,但简化的版本如下:

id <- c("id1", "id2", "id3", "id4")
A <- c("E", "F", "G", NA)
B <- c("T", NA, "N", "T")
C <- c(NA, "T", "U", NA)

df <- data.frame(A, B, C)

     id    A    B    C
1    id1   E    T    NA
2    id2   F    NA   T
3    id3   G    N    U
4    id4   NA   T    NA

我有一个要与df匹配的条目,例如:

df2 <- data.frame(A = "E", B = "T", C = "M")

    A    B    C
1   E    T    M

因此,我想从df中获取与df2匹配的所有行,但应忽略NA。因此结果应如下所示:

     id    A    B    C
1    id1   E    T    NA
2    id4   NA   T    NA

我试图用semi_join来做到这一点,但是到目前为止还没有奏效:

result <- df %>%
  group_by(n = seq(n())) %>%
  do(modify_if(., is.na, ~NULL) %>%
       semi_join(df2, by = c("A", "B", "C"))) %>%
  ungroup %>%
  select(-n)

这将导致:

Error: `by` can't contain join column `C` which is missing from LHS
Call `rlang::last_error()` to see a backtrace

谁知道答案?

3 个答案:

答案 0 :(得分:1)

这是一个混合了tidyverse和基数R的解决方案。我认为这很清楚,但是我会对纯tidyverse实现感兴趣,而这种实现并不是完全人为设计的。

这个想法是首先展开dfdf2中的所有条目,然后使用循环过滤所有列。

数据:

id <- c("id1", "id2", "id3", "id4")
A <- c("E", "F", "G", NA)
B <- c("T", NA, "N", "T")
C <- c(NA, "T", "U", NA)

df <- data.frame(id, A, B, C, stringsAsFactors = F) # Make sure to use strings not factors
df2 <- data.frame(A = "E", B = "T", C = "M", stringsAsFactors = F)

代码:

library(tidyr)
results <- crossing(df, df2)
select_columns <- c("A", "B", "C")
for(col in select_columns) {
  keep <- is.na(results[[col]]) | results[[col]] == results[[paste0(col, 1)]]
  results <- results[keep,, drop=F]
}
results <- results %>% dplyr::select(id, A:C) %>% distinct
results

   id    A B    C
1 id1    E T <NA>
2 id4 <NA> T <NA>

答案 1 :(得分:1)

如果只需要对一组值进行此操作,则这可能是最简单的方法:

d[A %in% c("E",NA) & B %in%c("T",NA) & C %in% c("M",NA),]

答案 2 :(得分:0)

另一个使用tidyverse和base(dplyr,tidyr,base)的示例:

在这种情况下,我将df2转换为包含要接受的值的所有组合((E或NA)&(T或NA)&(M或NA))的数据框,然后对此进行内部联接全套。还有其他方法可以创建所有可能组合的数据框,但这很容易使用tidyr。

library(dplyr)
library(tidyr)

id <- c("id1", "id2", "id3", "id4")
A <- c("E", "F", "G", NA)
B <- c("T", NA, "N", "T")
C <- c(NA, "T", "U", NA)

df <- data.frame(A, B, C, stringsAsFactors = FALSE)

df2 <- data.frame(A = "E", B = "T", C = "M",stringsAsFactors = FALSE)

df2_expanded <- df2 %>%
  rowwise() %>%
  mutate(combinations = list(expand.grid(A = c(A,NA),B = c(B,NA),C = c(C,NA),stringsAsFactors = FALSE))) %>%
  select(-A,-B,-C) %>%
  unnest(combinations)

# A tibble: 8 x 3
#   A     B     C    
# <chr> <chr> <chr>
# 1 E     T     M    
# 2 NA    T     M    
# 3 E     NA    M    
# 4 NA    NA    M    
# 5 E     T     NA   
# 6 NA    T     NA   
# 7 E     NA    NA   
# 8 NA    NA    NA   

df %>%
  inner_join(df2_expanded)

#      A B    C
# 1    E T <NA>
# 2 <NA> T <NA>