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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:06:15+00:00 2026-06-02T09:06:15+00:00

Following on from How to optimise filtering and counting for every row in a

  • 0

Following on from How to optimise filtering and counting for every row in a large R data frame

I have a data.table such as the following:

  name day wages hour colour
1  Ann   1   100    6  Green
2  Ann   1   150   18   Blue
3  Ann   2   200   10   Blue
4  Ann   3   150   10  Green
5  Bob   1   100   11    Red
6  Bob   1   200   17    Red
7  Bob   1   150   20  Green
8  Bob   2   100   11    Red

I wish to know, for every unique name/day pair, for one of four time-periods, a number of facts. The time periods I care about are:

t1 (hour < 9) 
t2 (hour < 17) 
t3 (hour > 9) 
t4 (hour > 17)

Some examples of facts might be:

wages > 175
colour = "Green"

I can accomplish this with the following data.table filter

setkey(dt,name,day)
result <- dt[,list(wages.t1=sum(wages>175&hour<9),
     wages.t2=sum(wages>175&hour<17),
     wages.t3=sum(wages>175&hour>9),
     wages.t4=sum(wages>175&hour>17),
     green.t1=sum(colour=="Green"&hour<9),
     green.t2=sum(colour=="Green"&hour<17),
     green.t3=sum(colour=="Green"&hour>9),
     green.t4=sum(colour=="Green"&hour>17)),

list(name,day)]

Giving me

     name day wages.t1 wages.t2 wages.t3 wages.t4 green.t1 green.t2 green.t3 green.t4
[1,]  Ann   1        0        0        0        0        1        1        0        0
[2,]  Ann   2        0        1        1        0        0        0        0        0
[3,]  Ann   3        0        0        0        0        0        1        1        0
[4,]  Bob   1        0        0        1        0        0        0        1        1
[5,]  Bob   2        0        0        0        0        0        0        0        0

But this a) Is horrible to read & write and b) Seems inefficient.

Any tips on how I can do better? Note that in my real scenario I have many hundreds of thousands of rows, four time periods, and 30-35 facts per time period.

— Code to create dt

dt = data.table(
  name = factor(c("Ann", "Ann", "Ann", "Ann", 
                  "Bob", "Bob", "Bob", "Bob")), 
  day = c(1, 1, 2, 3, 1, 1, 1, 2), 
  wages = c(100, 150, 200, 150, 100, 200, 150, 100), 
  hour = c(6, 18, 10, 10, 11, 17, 20, 11), 
  colour = c("Green", "Blue", "Blue", "Green", "Red",
             "Red", "Green", "Red")
)
  • 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-02T09:06:17+00:00Added an answer on June 2, 2026 at 9:06 am

    How about something like :

    f = list(quote(wages>175),quote(colour=="Green"))
    t = list(quote(hour<9),quote(hour<17),quote(hour>9),quote(hour>17))
    dt = as.data.table(df)
    dt[,as.list(mapply("%*%",
                lapply(t,eval,.SD),
                rep(lapply(f,eval,.SD),each=length(t))
               )), by=list(name,day)]
         name day V1 V2 V3 V4 V5 V6 V7 V8
    [1,]  Ann   1  0  0  0  0  1  1  0  0
    [2,]  Ann   2  0  1  1  0  0  0  0  0
    [3,]  Ann   3  0  0  0  0  0  1  1  0
    [4,]  Bob   1  0  0  1  0  0  0  1  1
    [5,]  Bob   2  0  0  0  0  0  0  0  0
    

    Clearly the column names aren’t tackled but that could be added if this approach is ok.

    This should be more efficient because each t and each f is evaluated once only per group, then the combinations of those results are combined.

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

Sidebar

Related Questions

i have the following script i am using to scrap data from my uni
I have the following query which obtains transactions from the transactions table and transaction
I have a problem following from my previous problem . I also have the
I have a query of the following form: SELECT * FROM MyTable WHERE Timestamp
Task is following: how to create selected picture mosaic from large number of images.
I'm looking for a way to optimise the following: SELECT (SELECT SUM(amount) FROM Txn_Log
I have a scenario to optimise how my web app is storing data in
Presently i have the following action to return files (images, PDF's, etc) from my
I have a table called 'Docs' whose primary key is a combination of following
I have the following query: SELECT AVG(time) FROM (SELECT UNIX_TIMESTAMP(max(datelast)) - UNIX_TIMESTAMP(min(datestart)) AS 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.