我有两个非常相似的数据框,试图将它们绑定在一起,但是遇到了问题。我使用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
列中的数据帧之间是不同的。
如何解决这两个问题,以使列类型一致?提前致谢!!
答案 0 :(得分: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