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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T05:20:46+00:00 2026-06-02T05:20:46+00:00

I have a data frame, such as the following: name day wages 1 Ann

  • 0

I have a data frame, such as the following:

  name day wages
1  Ann   1   100
2  Ann   1   150
3  Ann   2   200
4  Ann   3   150
5  Bob   1   100
6  Bob   1   200
7  Bob   1   150
8  Bob   2   100

For every unique name/day pair, I would like to calculate a range of totals, such as ‘number of times wages was greater than 175 on current or next day for this person’. There are many more columns than wages and there are four time-slices to be applied to each total for each row.

I can currently accomplish by unique’ing my data frame:

df.unique <- df[!duplicated(df[,c('name','day')]),]

And then for every row in df.unique, applying the following function (written longhand for clarity) to df:

for(i in 1:nrow(df.unique)) {
    df.unique[i,"wages_gt_175_day_and_next"] <- wages_gt_for_person_today_or_next(df,175,df.unique[i,"day"],df.unique[i,"name"])
}

wages_gt_for_person_today_or_next <- function(df,amount,day,person) {
  temp <- df[df$name==person,]
  temp <- temp[temp$day==day|temp$day==day+1,]
  temp <- temp[temp$wages > amount,]
  return(nrow(temp))
}

Giving me, in this trivial example:

name day wages_gt_175_day_and_next
Ann   1   1
Ann   2   1
Ann   3   0
Bob   1   1
Bob   2   0

However, this seems an extremely slow approach, given that I have hundreds of thousands of rows. Is there a cleverer way of doing this? Something with matrix operations, apply, sqldf, anything like that?

Code to recreate example df:

structure(list(name = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L), .Label = c("Ann", "Bob"), class = "factor"), day = c(1, 
1, 2, 3, 1, 1, 1, 2), wages = c(100, 150, 200, 150, 100, 200, 
150, 100)), .Names = c("name", "day", "wages"), row.names = c(NA, 
-8L), class = "data.frame")
  • 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-02T05:20:49+00:00Added an answer on June 2, 2026 at 5:20 am

    Going simply from your example output, here’s something a bit fancier using data.table:

    require(data.table)
    DT <- data.table(df)
    setkey(DT,name,day)
    
    DT[,list(gt175 = sum(wages >= 175)),list(name,day)][,list(day = day,gt175 = as.integer(gt175 + c(tail(gt175,-1),0) > 0)),list(name)]
    

    This is a little convoluted, but should be fast.

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

Sidebar

Related Questions

I have a set of data such as the following: annual_exp<-as.data.frame(c(6000,4200,240001,750,20000,3470,10500,2400,2280,36000,3600,20000,2000,12000,1200,3000,4500,64000)) annual_exp<-as.data.frame(annual_exp) I want
I have a data frame called VarChange containing 1005 variables such as: row.name SamplingEvent
I have following data.frame. Sorry for such a big piece of data df <-
I have the following network diagram: set.seed(1410) df<-data.frame( site.x=c(rep(a,4),rep(b,4),rep(c,4),rep(d,4)), site.y=c(rep(c(e,f,g,h),4)), bond.strength=sample(1:100,16, replace=TRUE)) library(igraph) df<-graph.data.frame(df)
I have a data frame with two columns. First column contains categories such as
I have a data frame that's about ts1[100, 2000] in dimension as follows: >
I have a large data frame with the following fields (example data). #dput(data) gives...
I have the following data.frame df<-data.frame(x=c(A,B,C),colour=c(0.3,0.6,0.9)) x colour 1 A 0.3 2 B 0.6
I have the following data frame: id<-c(1,2,3,4) date<-c(23-01-08,01-11-07,30-11-07,17-12-07) df<-data.frame(id,date) df$date2<-as.Date(as.character(df$date), format = %d-%m-%y) in
consider the following heatmap: df <- data.frame(a=rep(letters[1:10],10), b=rep(letters[1:10], each=10), c=sample(2,100, replace=TRUE)) ggplot(df, aes(a, b))

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.