我尝试汇总存储在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)
答案 0 :(得分: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
类。