Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9183097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:43:12+00:00 2026-06-17T18:43:12+00:00

Using data.table package, is it possible to summarise data preserving combinations of variables that

  • 0

Using data.table package, is it possible to summarise data preserving combinations of variables that do not appear in the input?

With plyr package I know how to do this with the .drop argument, for example:

require(plyr)
df <- data.frame(categories = c(rep("A",3), rep("B",3), rep("C",3)), groups = c(rep(c("X", "Y"),4), "Z"), values = rep(1, 9))

df1 <- ddply(df, c("categories","groups"), .drop = F, summarise, sum = sum(values))

output:

 categories groups sum
1          A      X   2
2          A      Y   1
3          A      Z   0
4          B      X   1
5          B      Y   2
6          B      Z   0
7          C      X   1
8          C      Y   1
9          C      Z   1

In this case I preserve all groups/categories combinations even if its sum is 0.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:43:14+00:00Added an answer on June 17, 2026 at 6:43 pm

    Great question. Here are two ways. They both use by-without-by.

    DT = as.data.table(df)
    setkey(DT,categories,groups)
    DT[CJ(unique(categories),unique(groups)), sum(values,na.rm=TRUE)]
    
       categories groups V1
    1:          A      X  2
    2:          A      Y  1
    3:          A      Z  0
    4:          B      X  1
    5:          B      Y  2
    6:          B      Z  0
    7:          C      X  1
    8:          C      Y  1
    9:          C      Z  1
    

    where CJ stands for Cross Join, see ?CJ. by-without-by just means that j gets executed on each group that each row of i joins to.

    Admitedly it looks tricky on first sight. The idea is that if you have a known subset of groups, this syntax is faster than grouping everything and then selecting just the results from that you need. But in this case you’d like everything anyway so there’s not much advantage, other than being able to lookup groups that don’t exist in the data (which you can’t do with by).

    Another way is to by first as normal, then join the CJ() result to that :

    DT[,sum(values),keyby='categories,groups'][CJ(unique(categories),unique(groups))]
       categories groups V1
    1:          A      X  2
    2:          A      Y  1
    3:          A      Z NA
    4:          B      X  1
    5:          B      Y  2
    6:          B      Z NA
    7:          C      X  1
    8:          C      Y  1
    9:          C      Z  1
    

    but then you get NA instead of the desired 0. Those could be replaced using set() if need be. The second way might be faster because the two unique calls are given much smaller input.

    Both methods can be wrapped up into small helper functions if you do this a lot.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to reset some input text fields, inside of a data table, using
Im using prawn to create pdfs that contain much data in table format and
I started using data.table package in R to boost performance of my code. I
Using the data.table package in R, I am trying to create a cartesian product
I can easy export table data by using DBMS_XMLGEN. But is there a package
I'm using the data.table package to speed up some summary statistic collection on a
I recently discovered the data.table package and was now wondering whether or not I
When using the data.table package, I am a bit unsure of when i need
I am using data.table package quite a bit. There are lots of examples of
Using a data table from http://datatables.net/ , How can i stop it from overflowing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.