Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinct in data.table as in dplyr

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))
like image 406
Fausto Carvalho Marques Silva Avatar asked Feb 03 '26 16:02

Fausto Carvalho Marques Silva


1 Answers

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")
] 
like image 99
Grant Avatar answered Feb 06 '26 04:02

Grant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!