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 8922365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:48:53+00:00 2026-06-15T06:48:53+00:00

I have a data.table with one key and about 100 numeric rows, one of

  • 0

I have a data.table with one key and about 100 numeric rows, one of which is set to key. I would like to create a new variable that contains summation of each numeric rows, grouped by key.

For example, my data right now is

ID Count1 Count2 Count3
1   1      3      0
1   3      3      3
2   1      2      1
3   1      1      2

What I would like to have is:

ID Count1 Count2 Count3
1   4      6      3
2   1      2      1
3   1      1      2

I have tried so many ways to get this. I know I can do:

Y <- X[, list(Count=sum(Count1), Count2=sum(Count2), Count3=sum(Count3), by = ID]

However, I have hundreds of variables, and I only get their names on a list. How should I go about handling this?

Thanks a lot for your help.

Here is a code to generate test data:

ID <-c(rep(210, 9), rep(3917,6))
Count1 <- c(1,1,0,1,3,1,4,1,1,1,1,1,1,0,1)
Count2 <- c(1,0,0,1,0,1,0,1,1,1,1,1,1,0,1)
Count3 <- c(1,0,0,1,0,1,0,1,1,1,1,1,1,0,1)
x <- data.table(ID, Count1, Count2, Count3)
setkey(x, ID)
  • 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-15T06:48:54+00:00Added an answer on June 15, 2026 at 6:48 am

    Your test data doesn’t match the example you gave, but regardless – you can take advantage of the fact that data.table() has an operator named .SD for “subset of data. So this should work:

    x[, lapply(.SD, sum), by = ID]
    #----
         ID Count Count2 Count3
    1:  210    13      5      5
    2: 3917     5      5      5
    

    This is actually covered in the FAQ: type vignette("datatable-faq", package="data.table") or find it online.

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

Sidebar

Related Questions

I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have to get data from Oracle Table in which I have one datefield
I have one large database table of request data, much like Apache request logs,
I have a data.table object similar to this one library(data.table) c <- data.table(CO =
I have loaded my database with one table under Data Connections in the Server
I have an application where I move data from one table to another on
I have a requirement to insert 12600 data in one table. The data is
I have created one jTable. I want to display the data into table from
I have to copy a bunch of data from one database table into another.

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.