【问题标题】:Joining two dataframes to remove NaN values in the first dataframe加入两个数据帧以删除第一个数据帧中的 NaN 值
【发布时间】:2019-07-15 07:52:45
【问题描述】:

我想合并两个数据框列。

我有 df1,它有一个特定的列 (df$col1)。此列有 1-100 行,某些行有 NA 值(比如说第 10、15、20、50、69 行)。

Dataframe 2 有 10,15,20,50,69 行。

是否可以将 DF2 合并到 df$col 以便仅 df$col 中的 NA 值由 DF2 填充..取决于每个数据集的索引号

我试过了,但得到的数据框看起来不像我想要的

merge(brfss2$pa1min_,df,by.x=1,by.y=1,all.x=TRUE,all.y=TRUE)

这是两个数据框

数据框1:

1 NA
2 110
3 NA
4 35
5 NA
6 120
7 280
8 30
9 240
10 260
11 322
12 NA

数据框 2:

1 2127.6
3 1403.0
5 198.0
12 112.8

【问题讨论】:

  • 尝试加入 library(data.table);setDT(df1)[df2, v2 := i.v2, on = .(v1)] 假设 'v1', 'v2' 作为两个数据集中的列。您是否遇到过第一列的某些重复元素具有值并且不想替换它的情况?
  • 也许我应该将此添加到问题 df1 实际上是来自现有数据框的列...没有重复
  • 还假设您在第一个数据集中有12 24,并且在 dataset2 中有一个值,那么它不应该被替换吗?
  • @akrun 不会发生 dataset2 仅具有与第一个数据集中的 NA 值对应的行的值
  • 好的,那么我的解决方案应该可以工作

标签: r dataframe merge


【解决方案1】:

我写了包safejoin,非常简洁地解决了这个问题

# devtools::install_github("moodymudskipper/safejoin")
safe_left_join(df1,df2, by = "col1", conflict = dplyr::coalesce)
# # A tibble: 12 x 2
#     col1  col2
#    <dbl> <dbl>
#  1     1 2128.
#  2     2  110 
#  3     3 1403 
#  4     4   35 
#  5     5  198 
#  6     6  120 
#  7     7  280 
#  8     8   30 
#  9     9  240 
# 10    10  260 
# 11    11  322 
# 12    12  113.

【讨论】:

    【解决方案2】:

    另一种方法 - 我导入了您的数据并给出了列名:

    df <- structure(list(col1 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
    ), col2 = c(NA, 110, NA, 35, NA, 120, 280, 30, 240, 260, 322, 
    NA)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, 
    -12L), spec = structure(list(cols = list(col1 = structure(list(), class = c("collector_double", 
    "collector")), col2 = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 2), class = "col_spec"))
    
    df2 <- structure(list(col1 = c(1, 3, 5, 12), col2 = c(2127.6, 1403, 
    198, 112.8)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
    ), row.names = c(NA, -4L), spec = structure(list(cols = list(
    col1 = structure(list(), class = c("collector_double", "collector"
    )), col2 = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 2), class = "col_spec"))
    

    使用tidyverse,您可以合并然后根据没有NA的值有条件地添加新列:

    library(tidyverse)
    
    df %>% 
      merge(df2, by = "col1", all.x = TRUE) %>% 
      mutate(new_col = if_else(is.na(col2.x), col2.y, col2.x)) %>% 
      select(new_col)
    
       new_col
    1     2127.6
    2     110.0
    3     1403.0
    4     35.0
    5     198.0
    6     120.0
    7     280.0
    8     30.0
    9     240.0
    10    260.0
    11    322.0
    12    112.8
    

    【讨论】:

    • 谢谢。但在这种情况下,出现在 col1 中的数字是索引
    • 然后为您的两个 dfs 使用 dplyr::rowid_to_column() 将提供要合并的列。
    • @Emm 索引号与列之间似乎有些混淆 - 您可能希望编辑您的问题以反映这一点或 dput 两个 dfs。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2018-04-06
    • 1970-01-01
    • 2020-10-09
    • 2020-06-22
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多