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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:06:50+00:00 2026-06-04T07:06:50+00:00

I have a data.frame that looks like this: ID Date.A Date.B Variable A 01/01/2012

  • 0

I have a data.frame that looks like this:

ID     Date.A        Date.B        Variable
A      01/01/2012    03/24/2012    Apples
A      02/01/2012    03/24/2012    Oranges
B      01/01/2012    02/04/2012    Bananas
C      01/01/2012    04/22/2012    Apples
A      03/01/2012    03/24/2012    Grapes
B      02/01/2012    02/04/2012    Oranges
D      01/01/2012    03/15/2012    Apples

For each ID, I need to find the Variable which corresponds to the first of the month previous to Date.B. So for ID A, I need to extract “Oranges”, because 03/24/2012 – 1 month = 02/24/2012, and the first of that month is 02/01/2012.

I have no problem with the date calculations using lubridate: format.Date(df$Date.B – months(1), format=”%Y-%m-01″). I’m having trouble incorporating that logic into ddply or in a programmatic manner without writing a nasty for loop. Not asking for the algorithmic help or for someone to solve it for me, but would love to see an example of ddply that uses custom date logic similar to this.

Thanks in advance.

  • 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-04T07:06:50+00:00Added an answer on June 4, 2026 at 7:06 am

    This is not with lubridate or plyr and I’m not even sure if it’s the solution to your problem. I chose to use the mondate package for the reasons tcash21 states:

    10/31/2012 - months(1)
    

    And I don’t use plyr much any more so I don’t really think in terms of plyr that much. if your data is called dat:

    #Read your data in
    dat <- read.table(text="ID     Date.A        Date.B        Variable
    A      01/01/2012    03/24/2012    Apples
    A      02/01/2012    03/24/2012    Oranges
    B      01/01/2012    02/04/2012    Bananas
    C      01/01/2012    04/22/2012    Apples
    A      03/01/2012    03/24/2012    Grapes
    B      02/01/2012    02/04/2012    Oranges
    D      01/01/2012    03/15/2012    Apples", header=TRUE)
    
    #This may be unnecessary but when I read your data in the dates were characters
    lapply(2:3,  function(i) {dat[, i] <<- as.Date(dat[, i], "%m/%d/%Y")})
    
    
    
    library(mondate)
    prevmon <- as.character(mondate(dat$Date.B) - 1)  #get minus 1 month
    z <- strsplit(prevmon, "-")                       #make it that first of that month
    dat$Date.C <- as.Date(sapply(z, function(x) paste(x[1], x[2], "01", sep="-")))
    
    key <- split(dat, dat$ID)                         #make list of data frames by ID 
    
    #fruit finding function
    fruiter <- function(x) x[match(x[, "Date.C"][1], x[, "Date.A"]), "Variable"]
    sapply(key, fruiter)
    

    Which yields:

          A       B       C       D 
    Oranges Bananas    <NA>    <NA> 
    

    Clearly there’s an NA for some months for the reason in the comment I attached to your question.

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

Sidebar

Related Questions

I have a semi-melted data frame that looks like this: head(final_melt) Group Source variable
I have a data frame that looks like this: site date var dil 1
Assuming I have a melted data.frame that looks like this: variable value 1 A
I have a data.frame that looks like this: > head(ff.df) .id pio caremgmt prev
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,
I have a data.frame that looks like this > head(df) Memory Memory Memory Memory
I have a data frame that looks like this: user1,product1,0 user1,product2,2 user1,product3,1 user1,product4,2 user2,product3,0
I have an R data frame that looks like this: z = as.data.frame(list(Col1=c(a,c,e,g),Col2=c(b,d,f,h),Col3=c(1,2,5,3,5,7,9,8,1))) >
I have a large data frame (named z ) that looks like this: RPos

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.