I'm trying to aggregate some data stored in a data.table
, and then create durations (from lubridate
) from the aggregated data. When I try that, however, I get an error. Here's a reproducible example:
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
Any ideas what this error means, or where it's coming from?
Edit:
This still happens when you leave out dplyr
and just do everything in data.table
. Here's the code I used:
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)
You can try by converting the
interval
output to eithercharacter
class (as theinterval
output is not avector
) or wrap withas.duration
(from @Jake Fisher)Or use
as.vector
which will coerce it tonumeric
class.