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

  • Home
  • SEARCH
  • 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 8507755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:57:34+00:00 2026-06-11T02:57:34+00:00

table with data (its a data.table object) that looks like the following : date

  • 0

table with data (its a data.table object) that looks like the following :

      date         stock_id logret
   1: 2011-01-01        1  0.001
   2: 2011-01-02        1  0.003
   3: 2011-01-03        1  0.005
   4: 2011-01-04        1  0.007
   5: 2011-01-05        1  0.009
   6: 2011-01-06        1  0.011
   7: 2011-01-01        2  0.013
   8: 2011-01-02        2  0.015
   9: 2011-01-03        2  0.017
  10: 2011-01-04        2  0.019
  11: 2011-01-05        2  0.021
  12: 2011-01-06        2  0.023
  13: 2011-01-01        3  0.025
  14: 2011-01-02        3  0.027
  15: 2011-01-03        3  0.029
  16: 2011-01-04        3  0.031
  17: 2011-01-05        3  0.033
  18: 2011-01-06        3  0.035

The above can be created as :

DT = data.table(
   date=rep(as.Date('2011-01-01')+0:5,3) , 
   stock_id=c(1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3),
  logret=seq(0.001, by=0.002, len=18));

setkeyv(DT,c('stock_id','date'))

Of course the real table is larger with many more stock_ids and dates. The aim to to reshape this data table such that I can run a regression of all stockid log_returns with their corresponding log_returns with a lag of 1 day (or prior traded day in case of weekends).

The final results would look like :

      date         stock_id logret lagret
   1: 2011-01-01        1  0.001    NA
   2: 2011-01-02        1  0.003    0.001
   3: 2011-01-03        1  0.005    0.003
   ....
  16: 2011-01-04        3  0.031  0.029
  17: 2011-01-05        3  0.033  0.031
  18: 2011-01-06        3  0.035  0.033

I’m finding this data structure really tricky to build without mixing up my stockid.

  • 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-11T02:57:35+00:00Added an answer on June 11, 2026 at 2:57 am

    Just some additional notes due to Alex’s comment. The reason you have difficulties understanding what’s going on here is that a lot of things are done within one line. So it’s always a good idea to break things down.

    What do we actually want? We want a new column lagret and the syntax to add a new column in data.table is the following:

    DT[, lagret := xxx]
    

    where xxx has to be filled up with whatever you want to have in column lagret. So if we just want a new column that gives us the rows, we could just call

    DT[, lagret := seq(from=1, to=nrow(DT))]
    

    Here, we actually want the lagged value of logret, but we have to consider that there are many stocks in here. That’s why we do a self-join, i.e. we join the data.table DT with itself by the columns stock_id and date, but since we want the previous value of each stock, we use date-1. Note that we have to set the keys first to do such a join:

    setkeyv(DT,c('stock_id','date'))
    DT[list(stock_id,date-1)]
        stock_id       date logret
     1:        1 2010-12-31     NA
     2:        1 2011-01-01  0.001
     3:        1 2011-01-02  0.003
     4:        1 2011-01-03  0.005
     5:        1 2011-01-04  0.007
     6:        1 2011-01-05  0.009
    ...
    

    As you can see, we now have what we want. logret is now lagged by one period. But we actually want that in a new column lagret in DT, so we just get that column by calling [[3L]] (this means nothing else then get me the third column) and name this new column lagret:

    DT[,lagret:=DT[list(stock_id,date-1),logret][[3L]]]
              date stock_id logret lagret
     1: 2011-01-01        1  0.001     NA
     2: 2011-01-02        1  0.003  0.001
     3: 2011-01-03        1  0.005  0.003
     4: 2011-01-04        1  0.007  0.005
     5: 2011-01-05        1  0.009  0.007
    ...
    

    This is already the correct solution. In this simple case, we do not need roll=TRUE because there are no gaps in the dates. However, in a more realistic example (as mentioned above, for instance when we have weekends), there might be gaps. So let’s make such a realistic example by just deleting two days in the DT for the first stock:

    DT <- DT[-c(4, 5)]
    setkeyv(DT,c('stock_id','date'))
    DT[,lagret:=DT[list(stock_id,date-1),logret][[3L]]]
              date stock_id logret lagret
     1: 2011-01-01        1  0.001     NA
     2: 2011-01-02        1  0.003  0.001
     3: 2011-01-03        1  0.005  0.003
     4: 2011-01-06        1  0.011     NA
     5: 2011-01-01        2  0.013     NA
    ...
    

    As you can see, the problem is now that we don’t have a value for the 6th of January. That’s why we use roll=TRUE:

    DT[,lagret:=DT[list(stock_id,date-1),logret,roll=TRUE][[3L]]]
              date stock_id logret lagret
     1: 2011-01-01        1  0.001     NA
     2: 2011-01-02        1  0.003  0.001
     3: 2011-01-03        1  0.005  0.003
     4: 2011-01-06        1  0.011  0.005
     5: 2011-01-01        2  0.013     NA
    ...
    

    Just have a look on the documentation on how roll=TRUE works exactly. In a nutshell: If it can’t find the previous value (here logret for the 5th of January), it just takes the last available one (here from the 3rd of January).

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

Sidebar

Related Questions

I have a data-structure (in plist) that looks something like this: What i have
I have a Javascript object that (very simplified) looks like this if('undefined' !== typeof(listCtrls)){
I have a function to sort a JSON object that looks like this: function
I have a db table that looks like this: State is a number that
When trying Edit Table Data in MySQL Workbench 5.2.37, its in read only mode.
is it possible to convert data table to ienumerable without know its class name.
I'm working on a trigger which needs to re-insert its data on another table.
I have a table of data that is being sorted alphabetically, and it's a
A site I'm working on takes table data, counts that table data and uses
I have a User table that has serialized data in one of the tables.

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.