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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:16:49+00:00 2026-06-16T02:16:49+00:00

I have a a big dataframe in R that all looks about like this:

  • 0

I have a a big dataframe in R that all looks about like this:

    name   amount   date1       date2  days_out year
    JEAN  318.5 1971-02-16 1972-11-27  650 days 1971
 GREGORY 1518.5       <NA>       <NA>   NA days 1971
    JOHN  318.5       <NA>       <NA>   NA days 1971
  EDWARD  318.5       <NA>       <NA>   NA days 1971
  WALTER  518.5 1971-07-06 1975-03-14 1347 days 1971
   BARRY 1518.5 1971-11-09 1972-02-09   92 days 1971
   LARRY  518.5 1971-09-08 1972-02-09  154 days 1971
   HARRY  318.5 1971-09-16 1972-02-09  146 days 1971
   GARRY 1018.5 1971-10-26 1972-02-09  106 days 1971

If someone’s days_out is less than 60, they get a 90% discount. 60-90, a 70% discount. I need to find out the discounted sum of all the amounts for each year. My utterly embarrassing workaround is to write a python script that writes an R script that reads like this for each relevant year:

tmp <- members[members$year==1971, ]
tmp90 <- tmp[tmp$days_out <= 60  & tmp$days_out > 0  & !is.na(tmp$days_out),  ]
tmp70 <- tmp[tmp$days_out <= 90  & tmp$days_out > 60 & !is.na(tmp$days_out),  ]
tmp50 <- tmp[tmp$days_out <= 120 & tmp$days_out > 90 & !is.na(tmp$days_out),  ]
tmp30 <- tmp[tmp$days_out <= 180 & tmp$days_out >120 & !is.na(tmp$days_out),  ]
tmp00 <- tmp[tmp$days_out > 180 | is.na(tmp$days_out), ]
details.1971 <- c(1971, nrow(tmp),
  nrow(tmp90), sum(tmp90$amount), sum(tmp90$amount) * .9,
    nrow(tmp70), sum(tmp70$amount), sum(tmp70$amount) * .7,
    nrow(tmp50), sum(tmp50$amount), sum(tmp50$amount) * .5,
    nrow(tmp30), sum(tmp30$amount), sum(tmp90$amount) * .9,
    nrow(tmp00), sum(tmp00$amount))
membership.for.chart <- rbind(membership.for.chart,details.1971)

It works just fine. The tmp frames and vectors get overwritten which is fine. But I know that I’ve utterly defeated everything that is elegant and efficient about R here. I launched R for the first time a month ago and I think I’ve come a long way. But I would really like to know how I should have gone about this?

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

    Wow, you wrote a Python script that generates an R script? Consider my eyebrows raised…

    Hopefully this will get you started:

    #Import your data; add dummy column to separate 'days' suffix into its own column
    dat <- read.table(text = "   name   amount   date1       date2  days_out dummy year
        JEAN  318.5 1971-02-16 1972-11-27  650 days 1971
     GREGORY 1518.5       <NA>       <NA>   NA days 1971
        JOHN  318.5       <NA>       <NA>   NA days 1971
      EDWARD  318.5       <NA>       <NA>   NA days 1971
      WALTER  518.5 1971-07-06 1975-03-14 1347 days 1971
       BARRY 1518.5 1971-11-09 1972-02-09   92 days 1971
       LARRY  518.5 1971-09-08 1972-02-09  154 days 1971
       HARRY  318.5 1971-09-16 1972-02-09  146 days 1971
       GARRY 1018.5 1971-10-26 1972-02-09  106 days 1971",header = TRUE,sep = "")
    
    #Repeat 3 times
    df <- rbind(dat,dat,dat)
    
    #Create new year variable
    df$year <- rep(1971:1973,each = nrow(dat))
    
    #Breaks for discount levels
    ct <- c(0,60,90,120,180,Inf)
    
    #Cut into a factor
    df$fac <- cut(df$days_out,ct)
    
    #Create discount amounts for each row
    df$discount <- c(0.9,0.7,0.5,0.9,1)[df$fac]
    df$discount[is.na(df$discount)] <- 1
    
    #Calc adj amount
    df$amount_adj <- with(df,amount * discount)
    
    #I use plyr a lot, but there are many, many
    # alternatives
    library(plyr)
    ddply(df,.(year),summarise,
                amt = sum(amount_adj),
                total = length(year),
                d60 = length(which(fac == "(0,60]")))
    

    I only calculated a few of your summary values in the last ddply command. I’m assuming you can extend it yourself.

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

Sidebar

Related Questions

I have a big dataframe, but small example would be like this: mydf <-
I have a very big data frame consisting of data like this: PENR ANFDAT
I have big collection of tweets stored in MongoDB. Tweets look like this one:
I have big .csv file. I would like to filter that file into a
I have big system that make my system crash hard. When I boot up,
I have big element at the top of the webpage that sides down with
I have a big dataframe with columns such as: ID, time, OS, IP Each
I have a small webshop-like web application, and i have big plans :-) I
I have a big data frame taking about 900MB ram. Then I tried to
I have big problems using the InternetGetLastResponseInfo function because of all the weird file

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.