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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:50:49+00:00 2026-06-06T20:50:49+00:00

I am doing the classic split-apply-recombine thing in R. My data set is a

  • 0

I am doing the classic split-apply-recombine thing in R. My data set is a bunch of firms over time. The applying I am doing is running a regression for each firm and returning the residuals, therefore, I am not aggregating by firm. plyr is great for this but it takes a very very long time to run when the number of firms is large. Is there a way to do this with data.table?

Sample Data:

dte, id, val1, val2
2001-10-02, 1, 10, 25
2001-10-03, 1, 11, 24
2001-10-04, 1, 12, 23
2001-10-02, 2, 13, 22
2001-10-03, 2, 14, 21

I need to split by each id (namely 1 and 2). Run a regression, return the residuals and append it as a column to my data. Is there a way to do this using 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-06T20:50:50+00:00Added an answer on June 6, 2026 at 8:50 pm

    I’m guessing this needs to be sorted by “id” to line up properly. Luckily that happens automatically when you set the key:

    dat <-read.table(text="dte, id, val1, val2
     2001-10-02, 1, 10, 25
     2001-10-03, 1, 11, 24
     2001-10-04, 1, 12, 23
     2001-10-02, 2, 13, 22
     2001-10-03, 2, 14, 21
     ", header=TRUE, sep=",")
     dtb <- data.table(dat)
     setkey(dtb, "id")
     dtb[, residuals(lm(val1 ~ val2)), by="id"]
    #---------------
    cbind(dtb, dtb[, residuals(lm(val1 ~ val2)), by="id"])
    #---------------
                dte id val1 val2 id.1            V1
    [1,] 2001-10-02  1   10   25    1  1.631688e-15
    [2,] 2001-10-03  1   11   24    1 -3.263376e-15
    [3,] 2001-10-04  1   12   23    1  1.631688e-15
    [4,] 2001-10-02  2   13   22    2  0.000000e+00
    [5,] 2001-10-03  2   14   21    2  0.000000e+00
    
    
    
    > dat <- data.frame(dte=Sys.Date()+1:1000000, 
                        id=sample(1:2, 1000000, repl=TRUE),  
                        val1=runif(1000000),  val2=runif(1000000) )
    > dtb <- data.table(dat)
    > setkey(dtb, "id")
    > system.time(  cbind(dtb, dtb[, residuals(lm(val1 ~ val2)), by="id"]) )
       user  system elapsed 
      1.696   0.798   2.466 
    > system.time( dtb[,transform(.SD,r = residuals(lm(val1~val2))),by = "id"] )
       user  system elapsed 
      1.757   0.908   2.690 
    

    EDIT from Matthew :
    This is all correct for v1.8.0 on CRAN. With the small addition that transform in j is the subject of data.table wiki point 2: “For speed don’t transform() by group, cbind() afterwards”. But, := now works by group in v1.8.1 and is both simple and fast. See my answer for illustration (but no need to vote for it).

    Well, I voted for it. Here is the console command to install v 1.8.1on a Mac (if you have the proper XCode tools avaialble, since it only there in source):

    install.packages("data.table", repos= "http://R-Forge.R-project.org", type="source", 
                   lib="/Library/Frameworks/R.framework/Versions/2.14/Resources/lib")
    

    (For some reason I could not get the Mac GUI Package Installer to read r-forge as a repository.)

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

Sidebar

Related Questions

Doing some jquery animation. I have certain divs set up with an attribute of
My ASP Classic application are fetching over 10.000 rows via a triple inner join.
After years of doing a little programming on the side (Classic ASP for 12
Hello I'm learning Objective C and I was doing the classic Calculator example. Problem
I am doing some twitter authentication in classic asp (I know, but I can't
We have an ASP Classic website running on Windows Server 2003 and IIS6 which
I'm doing some revision on an old app that is written in classic ASP/VbScript.
How do I access the classic Internet Explorer COM automation object for a running
I am doing web development using CFEclipse with Classic Eclipse (Indigo) on a Windows
I was doing a simple query using explain to know precisely the execution time.

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.