2

I am trying to use data.table for a better performance but dont know how to do the equivalent of distinct %>% summarize in dplyr. Any ideas how I could adapt the following code to data.table?

group_by_('x,y,z') %>%
distinct('h', .keep_all = TRUE) %>%
summarise(tot1 = sum(value1), tot2 = sum(value2))
divibisan
  • 11,659
  • 11
  • 40
  • 58

1 Answers1

5

You can do the group, distinct, and sum in 2 steps with data.table. First, use unique() with the by argument set to your grouping and distinct variables. Then do the data.table equivalent of summarize() with just the grouping variables.

dfq = data_frame(
    g1 = rep(c('a', 'b', 'c'), times = 12), 
    g2 = rep(c('d', 'e', 'f', 'g'), times = 9), 
    c3 = as.integer(30 * runif(36)), 
    d4 = rep(LETTERS[1:18], times = 2)
)

dtq = as.data.table(dfq)
dtq2 = unique(dtq, by = c("g1", "g2", "d4"))[
    , .(sum1 = sum(c3)), 
    by = c("g1", "g2")
] 
Grant
  • 346
  • 2
  • 12