简体   繁体   English

R中的hms difftime类型的问题

[英]Issue with hms difftime type in R

I have two dataframes that are very similar that I am trying to rbind together, but am running into an issue. 我有两个非常相似的数据框,试图将它们绑定在一起,但是遇到了问题。 I used dput() to grab 3 columns (one of which is problematic) and 10 rows from each dataframe. 我使用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

When I perform rbind.fill the clock column turns into NA:NA:NANA, which is frustrating. 当我执行rbind.fill ,时钟列变成NA:NA:NANA,这令人沮丧。 When I check the classes for the clock column in each dataframe, they "appear" to be the same: 当我检查每个数据帧中clock列的类时,它们“看起来”是相同的:

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

...however, what the dput() has fortunately revealed to me is that the values in the clock vector are strings for str1 and numbers for str2. ...但是,幸运的是dput()向我揭示的是,时钟向量中的值是str1的字符串和str2的数字。 Again I did not create these demo str dataframes from scratch, they are from my full dataframes, so this is clearly a different in the clock column between dataframes. 再次,我没有从头开始创建这些demo str数据帧,它们是从我的完整数据帧创建的,因此这在数据帧之间的clock列中显然是不同的。

How can I fix either of these so that the column types are consistent? 我该如何解决这些问题,以使列类型保持一致? Thanks in advance!! 提前致谢!!

This is not really an explanation why plyr::rbind.fill did not work, but the following does work 这是不是真的,为什么解释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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM