0

我有两个非常相似的数据框,我试图将它们绑定在一起,但遇到了问题。我使用 dput() 从每个数据帧中抓取 3 列(其中一个有问题)和 10 行。

str1 = structure(list(period_type = c("half", "half", "half", "half", 
                               "half", "half", "half", "half", "half", "half"), period_number = c(1L, 
                                                                                                  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), clock = structure(c("72000", 
                                                                                                                                                           "70800", "69720", "69600", "69480", "68280", "67200", "66780", 
                                                                                                                                                           "65160", "65160"), class = c("hms", "difftime"), units = "secs")), row.names = c(NA, 
                                                                                                                                                                                                                                            10L), class = "data.frame")

str2 = structure(list(period_type = c("half", "half", "half", "half", 
                               "half", "half", "half", "half", "half", "half"), period_number = c(1L, 
                                                                                                  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), clock = structure(c(72000, 
                                                                                                                                                           71640, 70140, 70020, 69720, 69720, 69720, 69720, 69300, 67860
                                                                                                  ), class = c("hms", "difftime"), units = "secs")), row.names = c(NA, 
                                                                                                                                                                   10L), class = "data.frame")

> head(plyr::rbind.fill(str1, str2))
  period_type period_number      clock
1        half             1 NA:NA:NANA
2        half             1 NA:NA:NANA
3        half             1 NA:NA:NANA
4        half             1 NA:NA:NANA
5        half             1 NA:NA:NANA
6        half             1 NA:NA:NANA

当我执行rbind.fill时钟列时变成 NA:NA:NANA,这令人沮丧。当我检查clock每个数据框中列的类时,它们“看起来”是相同的:

> class(str1$clock)
[1] "hms"      "difftime"
> class(str2$clock)
[1] "hms"      "difftime"

...但是,dput()幸运的是,时钟向量中的值是 str1 的字符串和 str2 的数字。同样,我没有str从头开始创建这些演示数据框,它们来自我的完整数据框,因此这clock在数据框之间的列中显然是不同的。

如何修复其中任何一个以使列类型保持一致?提前致谢!!

4

1 回答 1

1

这并不是真正的解释为什么plyr::rbind.fill不起作用但以下确实有效

library(hms)
do.call(rbind, list(str1, str2))
#   period_type period_number    clock
#1         half             1 20:00:00
#2         half             1 19:40:00
#3         half             1 19:22:00
#4         half             1 19:20:00
#5         half             1 19:18:00
#6         half             1 18:58:00
#7         half             1 18:40:00
#8         half             1 18:33:00
#9         half             1 18:06:00
#10        half             1 18:06:00
#11        half             1 20:00:00
#12        half             1 19:54:00
#13        half             1 19:29:00
#14        half             1 19:27:00
#15        half             1 19:22:00
#16        half             1 19:22:00
#17        half             1 19:22:00
#18        half             1 19:22:00
#19        half             1 19:15:00
#20        half             1 18:51:00
于 2019-05-03T01:49:23.693 回答