簡體   English   中英

使用lubridate和dplyr從data.table創建的持續時間錯誤

[英]Error with durations created from a data.table using lubridate & dplyr

我正在嘗試聚合存儲在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)

您可以嘗試通過將interval輸出轉換為character類(因為interval輸出不是vector )或使用as.durationas.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類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM