无法按日期时间合并/合并两个数据框

时间:2019-03-15 15:30:39

标签: r merge dplyr

我正在尝试按日期时间联接两个数据框,但未成功。我检查了秒数,它们似乎匹配了,但内部联接未返回结果。我将mergetidyverse用于相同的输出。我在下面的脚本中包含了我的实际对象的示例:

library(tidyverse)

a <- structure(
  list(
    date = structure(c(
      126230400, 126234000, 126237600,
      126241200, 126244800, 126248400, 126252000, 126255600, 126259200,
      126262800, 126266400, 126270000, 126273600, 126277200, 126280800,
      126284400, 126288000, 126291600, 126295200, 126298800, 126302400,
      126306000, 126309600, 126313200
    ), class = c("POSIXct", "POSIXt"), tzone = "UTC"), wd = c(
      303, 317, 309, 312, 274, 298, 311,
      298, 314, 303, 303, 302, 305, 303, 277, 281, 284, 276, 283, 289,
      262, 284, 281, 287
    ), ws = c(
      4.6, 5.2, 4.6, 4.1, 3.1, 4.1, 2.6,
      4.6, 6.2, 2.1, 3.1, 4.6, 4.1, 4.6, 4.6, 5.2, 4.1, 4.1, 4.1, 4.1,
      4.1, 4.6, 3.6, 3.6
    )
  ),
  row.names = c(NA, -24L), class = c(
    "tbl_df",
    "tbl", "data.frame"
  )
)

b <- structure(
  list(
    date = structure(c(
      126230400, 126234000, 126237600,
      126259200, 126262800, 126266400, 126270000, 126273600, 126277200
    ), class = c("POSIXct", "POSIXt"), tzone = "UTC"), wd = c(
      303,
      321, 292, 291, 309, 296, 282, 285, 288
    ), ws = c(
      3.6525524, 3.85833,
      4.6814404, 4.115552, 4.0126632, 3.4467748, 6.0189948, 5.7103284,
      4.9386624
    )
  ),
  row.names = c(NA, -9L), class = c(
    "tbl_df", "tbl",
    "data.frame"
  )
)

a %>% inner_join(b)

1 个答案:

答案 0 :(得分:1)

我认为您正在使用的内容尝试使用所有变量作为键来合并。试试这个:

c <- inner_join(a,b, by = "date")

希望有帮助