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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:20:12+00:00 2026-05-27T18:20:12+00:00

I am sorry for the cryptic title but I didn’t know how to adequately

  • 0

I am sorry for the cryptic title but I didn’t know how to adequately summarise my problem. So here’s my question. I have a data frame with dates and a name for several entities:

 df <- data.frame(
       time=rep(as.Date(seq(as.Date("2004/1/1"), as.Date("2005/12/1"), by = "1 month ")),2),
       name=c(rep("a",24),rep("b",24))
  )
  str(df)

  'data.frame':   48 obs. of  2 variables:
   $ time: Date, format: "2004-01-01" "2004-02-01" ...
   $ name: Factor w/ 2 levels "a","b": 1 1 1 1 1 1 1 1 1 1 ...

And I have another dataframe with several unevenly spaced events:

   events <- data.frame(
          time = c("2004-12-1", "2005-8-1", "2005-6-1", "2004-4-1"),
          event = c("normal", "extraordinary", "normal", "extraordinary"),
          name = c("a", "a", "b", "b")
   )

I want to merge these two data frames in a way that the event is assigned from the either the beginning of the data set up to the event or starting with the last event up to the next event or the end of the data set. This would look something like:

    date      name  event
   2004-01-01  a     normal
   2004-01-02  a     normal 
      ...
   2004-12-01  a     extraordinary
   2005-01-01  a     extraordinary

Is there an easy way doing this in R that I don’t see or do I merge these by hand? Thank you very much for your help!

  • 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-05-27T18:20:13+00:00Added an answer on May 27, 2026 at 6:20 pm

    I don’t know any function to do this, but here is some R code to do it yourself :

    # Needed type coercions (Date for comparisons, characters to avoid 'factor' problems)
    events$time <- as.Date(events$time)
    events$event <- as.character(events$event)
    events$name <- as.character(events$name)
    df$name <- as.character(df$name)
    
    # Events ordering (needed to detect previous events as non NA)
    events <- events[ order(events$time) ,]
    
    # Updates
    df$event = NA
    for(i in 1:nrow(events)) {
        # Update where time is lesser than the limit, if names correspond and if an event was not already assigned to the row
        df[ df$time <= events[i,"time"] & df$name == events[i,"name"] & is.na(df$event) , "event" ] = events[i,"event"]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

sorry for the cryptic question title. I'm having a strange problem and i have
sorry, for the cryptic title i didn't find any better summary for my problem.
Sorry for the vague title, I didn't know how else to phrase this. Suppose
Sorry for the crappy title - didn't know how else to say it. I
sorry for lamer question, but I really could not found subject. I have a
Sorry the title is not very descriptive but it is a tricky problem to
sorry but i cant solve my problem, you know , Im a noob. I
Sorry to bother you folks. But i have a simple question. Ho can I
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for this not being a real question, but Sometime back i remember seeing

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.