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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:59:18+00:00 2026-06-06T13:59:18+00:00

I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,

  • 0

I have a data.table object like this one

library(data.table)

a <- structure(list(PERMNO = c(10006L, 10006L, 10015L, 10015L, 20000L, 20000L), 
                    SHROUT = c(1427L, 1427L, 1000L, 1001L, 200L, 200L), 
                    PRC = c(6.5, 6.125, 0.75, 0.5, 3, 4), 
                    RET = c(0.005, -0.005, -0.001, 0.05, -0.002, 0.0031)),
                   .Names = c("PERMNO", "SHROUT", "PRC", "RET"), 
               class = c("data.table", "data.frame"), row.names = c(NA, -6L))

setkey(a,PERMNO)

and I need to perform a number of calculations by PERMNO, but here in this example let’s supposed they are only 2:

mktcap <- a[ , tail(SHROUT,n=1)*tail(PRC,n=1),by=PERMNO]
sqret <- a[, sum(RET^2),by=PERMNO]

which produce

> mktcap
     PERMNO       V1
[1,]  10006 8740.375
[2,]  10015  500.500
[3,]  20000  800.000

> sqret
     PERMNO        V1
[1,]  10006 5.000e-05
[2,]  10015 2.501e-03
[3,]  20000 1.361e-05

I would like to combine the two functions into one, to produce a matrix (or data.table, data.frame, whatever) with 3 columns, the first with the PERMNOs, the second with mktcap and the third with sqrt.

The problem is that this grouping function (i.e. variable[ , function(), by= ]) seems to only produce results with two columns, one with the keys and one with results.

This is my attempt (one of many) to produce what I want:

comb.fun <- function(datai) {
     mktcap <- as.matrix(tail(datai[,1],n=1)*tail(datai[,2],n=1),ncol=1)
     sqret <- as.matrix(sum(datai[,3]^2),ncol=1)
     return(c(mktcap,sqret))
}   

myresults <- a[, comb.fun(cbind(SHROUT,PRC,RET)), by=PERMNO]

which produces

     PERMNO           V1
[1,]  10006 8.740375e+03
[2,]  10006 5.000000e-05
[3,]  10015 5.005000e+02
[4,]  10015 2.501000e-03
[5,]  20000 8.000000e+02
[6,]  20000 1.361000e-05

(the results are all there, but they were forced into one column). No matter what I try, I cannot get grouping to return a matrix with more than two columns (or more than one column of results).

Is it possible to get two or more column of results with grouping in data.table?

  • 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-06T13:59:20+00:00Added an answer on June 6, 2026 at 1:59 pm

    The answer (using list() to collect the several desired summary stats) is there in the excellent Examples section of the ?data.table help file. (It’s about 20 lines up from the bottom).

    out <- a[ , list(mktcap = tail(SHROUT,n=1)*tail(PRC,n=1),
                     sqret  = sum(RET^2)),
             by=PERMNO]
    
    out
    #    PERMNO   mktcap     sqret
    # 1:  10006 8740.375 5.000e-05
    # 2:  10015  500.500 2.501e-03
    # 3:  20000  800.000 1.361e-05
    

    Edit:

    In the comments below, Matthew Dowle describes a simple way to clean up code in which the j argument in calls like x[i,j,by] is getting awkwardly long.

    Implementing his suggestion on the call above, you could instead do:

    ## 1) Use quote() to make an expression object out of the statement passed to j
    mm <- quote(list(mktcap = tail(SHROUT,n=1)*tail(PRC,n=1),
                     sqret  = sum(RET^2)))
    
    ## 2) Use eval() to evaluate it as if it had been typed directly in the call
    a[ , eval(mm), by=PERMNO]
    #    PERMNO   mktcap     sqret
    # 1:  10006 8740.375 5.000e-05
    # 2:  10015  500.500 2.501e-03
    # 3:  20000  800.000 1.361e-05
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit confused on this. I have a data table structured like this:
Let’s say I have table/list like this n=3 in this case, but n can
I have a table with data exactly like this: ID Heading Description Time Location
I have data table containing one column as FilePath. FilePath D:\New folder\link.txt D:\New folder\SharepointMigration(Work
I have a User table that has serialized data in one of the tables.
I have a function like this in one of my classes and then I
I have a function like this defined in one class: using System; using System.Collections.Generic;
I have a problem. Imagine this data model: [Person] table has: PersonId, Name1 [Tag]
I have a status table, and another table containing additional data. My object IDs
I have an object in C# like this: private ClassWidget { public int ID;

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.