【问题标题】:Error with durations created from a data.table using lubridate & dplyr使用 lubridate 和 dplyr 从 data.table 创建的持续时间错误
【发布时间】:2015-06-18 17:31:12
【问题描述】:

我正在尝试聚合存储在 data.table 中的一些数据,然后根据聚合数据创建持续时间(来自 lubridate)。但是,当我尝试这样做时,我得到了一个错误。这是一个可重现的示例:

library(lubridate)
library(data.table)
library(dplyr)

data(lakers)
lakers.dt <- data.table(lakers, key = "player")

durations <- lakers.dt %>%
  mutate(better.date = ymd(date)) %>%
  group_by(player) %>%
  summarize(min.date = min(better.date), max.date = max(better.date)) %>%
  mutate(duration = interval(min.date, max.date))

# Source: local data table [371 x 4]
# 
# player   min.date   max.date
# 1                2008-10-28 2009-04-14
# 2   Aaron Brooks 2008-11-09 2009-04-03
# 3     Aaron Gray 2008-11-18 2008-11-18
# 4       Acie Law 2009-02-17 2009-02-17
# 5  Adam Morrison 2009-02-17 2009-04-12
# 6  Al Harrington 2008-12-16 2009-02-02
# 7     Al Horford 2009-02-17 2009-03-29
# 8   Al Jefferson 2008-12-14 2009-01-30
# 9    Al Thornton 2008-10-29 2009-04-05
# 10 Alando Tucker 2009-02-26 2009-02-26
# ..           ...        ...        ...
# Variables not shown: duration (dbl)
# Warning messages:
#   1: In unclass(e1) + unclass(e2) :
#   longer object length is not a multiple of shorter object length
# 2: In format.data.frame(df, justify = "left") :
#   corrupt data frame: columns will be truncated or padded with NAs

知道这个错误是什么意思,或者它来自哪里?

编辑:

当您忽略 dplyr 并在 data.table 中执行所有操作时,仍然会发生这种情况。这是我使用的代码:

lakers.dt[, better.date := ymd(date)]
durations <- lakers.dt[, list(min.date = min(better.date),
                              max.date = max(better.date)), by = player]
(durations[, duration := interval(min.date, max.date)])
# Error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = TRUE),  : 
#   length of 'dimnames' [1] not equal to array extent
# In addition: Warning messages:
# 1: In unclass(e1) + unclass(e2) :
#   longer object length is not a multiple of shorter object length
# 2: In cbind(player = c("", "Aaron Brooks", "Aaron Gray", "Acie Law",  :
#   number of rows of result is not a multiple of vector length (arg 1)

【问题讨论】:

  • 谢谢。我不确定你在summarise 之后所说的do 是什么意思,但是当你完全在data.table 中执行此操作时,它仍然会发生。我会更新帖子以反映这一点。
  • 我认为interval( 的输出不是向量。 is.vector(new_interval(ymd(20090101), ymd(20090201))) #[1] FALSE 它有 3 个插槽
  • @akrun 这可能是问题所在,谢谢。我想我可以将相隔天数保存为数字变量或类似的东西。
  • 你为什么首先在这里使用interval?它增加了什么价值?您可以只使用paste,例如durations[, duration := paste(min.date, max.date)]。另外,当在data.table 中使用:= 时,你是在原地修改,所以durations &lt;- lakers.dt 没有真正的意义,因为你也更新了lakers.dt
  • @DavidArenburg 哎呀!你是对的,:= 没有做我认为它正在做的事情。我试图按玩家汇总。我更新了它,现在它应该可以正常运行了。

标签: r data.table dplyr lubridate


【解决方案1】:

您可以尝试将interval 输出转换为character 类(因为interval 输出不是vector)或用as.duration 包装(来自@Jake Fisher)

durations <- lakers.dt %>%
        mutate(better.date = ymd(date)) %>%
        group_by(player) %>%
        summarize(min.date = min(better.date), max.date = max(better.date)) %>%
        mutate(duration= as.duration(interval(min.date, max.date))
     )

或使用 as.vector 将其强制转换为 numeric 类。

【讨论】:

  • 是的,这解决了问题,谢谢。将其存储为字符变量不如将其存储为持续时间有用,至少对我而言。所以我更喜欢mutate(duration = as.duration(interval(min.date, max.date)))
  • @JakeFisher 是的,as.vector 也将其强制到持续时间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-12
  • 2019-10-23
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
相关资源
最近更新 更多