r - Error with durations created from a data.table using lubridate & dplyr -


i'm trying aggregate data stored in data.table, , create durations (from lubridate) aggregated data. when try that, however, error. here's 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 not multiple of shorter object length # 2: in format.data.frame(df, justify = "left") : #   corrupt data frame: columns truncated or padded nas 

any ideas error means, or it's coming from?

edit:

this still happens when leave out dplyr , in data.table. here's code used:

lakers.dt[, better.date := ymd(date)] durations <- lakers.dt[, list(min.date = min(better.date),                               max.date = max(better.date)), = player] (durations[, duration := interval(min.date, max.date)]) # error in `rownames<-`(`*tmp*`, value = paste(format(rn, right = true),  :  #   length of 'dimnames' [1] not equal array extent # in addition: warning messages: # 1: in unclass(e1) + unclass(e2) : #   longer object length not multiple of shorter object length # 2: in cbind(player = c("", "aaron brooks", "aaron gray", "acie law",  : #   number of rows of result not multiple of vector length (arg 1) 

you can try converting interval output either character class (as interval output not vector) or wrap as.duration (from @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))      ) 

or use as.vector coerce numeric class.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -