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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:06:43+00:00 2026-06-14T00:06:43+00:00

I have a data.table which contains multiple columns, which is well represented by the

  • 0

I have a data.table which contains multiple columns, which is well represented by the following:

DT <- data.table(date = as.IDate(rep(c("2012-10-17", "2012-10-18", "2012-10-19"), each=10)), 
                   session = c(1,2,3), price = c(10, 11, 12,13,14), 
                   volume = runif(30, min=10, max=1000)) 

I would like to extract a multiple column table which shows the volume traded at each price in a particular type of session — with each column representing a date.

At present, i extract this data one date at a time using the following:

DT[session==1,][date=="2012-10-17", sum(volume), by=price]

and then bind the columns.

Is there a way of obtaining the end product (a table with each column referring to a particular date) without sticking all the single queries together — as i’m currently doing?

thanks

  • 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-14T00:06:45+00:00Added an answer on June 14, 2026 at 12:06 am

    Does the following do what you want.

    A combination of reshape2 and data.table

    library(reshape2)
    
    .DT <- DT[,sum(volume),by = list(price,date,session)][, DATE := as.character(date)]
    # reshape2 for casting to wide -- it doesn't seem to like IDate columns, hence
    # the character DATE co
    dcast(.DT, session + price ~ DATE, value.var = 'V1')
    
        session price 2012-10-17 2012-10-18 2012-10-19
    1        1    10   308.9528   592.7259         NA
    2        1    11   649.7541         NA   816.3317
    3        1    12         NA   502.2700   766.3128
    4        1    13   424.8113   163.7651         NA
    5        1    14   682.5043         NA   147.1439
    6        2    10         NA   755.2650   998.7646
    7        2    11   251.3691   695.0153         NA
    8        2    12   791.6882         NA   275.4777
    9        2    13         NA   111.7700   240.3329
    10       2    14   230.6461   817.9438         NA
    11       3    10   902.9220         NA   870.3641
    12       3    11         NA   719.8441   963.1768
    13       3    12   361.8612   563.9518         NA
    14       3    13   393.6963         NA   718.7878
    15       3    14         NA   871.4986   582.6158
    

    If you just wanted session 1

    dcast(.DT[session == 1L], session + price ~ DATE)
    
       session price 2012-10-17 2012-10-18 2012-10-19
    1        1    10   308.9528   592.7259         NA
    2        1    11   649.7541         NA   816.3317
    3        1    12         NA   502.2700   766.3128
    4        1    13   424.8113   163.7651         NA
    5        1    14   682.5043         NA   147.1439
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, I have a table which contains multiple rows. Each row contains some data
I have a huge data.table in R which contains the result of a experiment:
I have a table which contains TV Guide data. In a simplified form, the
I have a table which contains data about which node has been visited. It
I have a database table in Sql Server 2008 R2 which contains data stored
I have a table with multiple tbody's, each of which has a classed row,
I have an (access) database table which contains data I would like to populate
I have a dynamic data table in JSF which has multiple input elements which
I have an invoices and a payments table. For each invoice record (which contains
Let's say I have a table which can contains data like this: id name

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.