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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:31:51+00:00 2026-06-17T10:31:51+00:00

I have the following data frame, id, date, state 1 2012-01-01 a 1 2012-01-02

  • 0

I have the following data frame,

id, date, state
1   2012-01-01 a
1   2012-01-02 a
1   2012-01-03 a
1   2012-01-04 b
1   2012-01-05 b
2   2013-01-01 a
2   2013-01-02 a
2   2013-01-03 b
2   2013-01-04 b

For each id, I want to find the date when the state changed from a to b following which I want it inserted as a column for that id. So the above example would yield

id, date, state, changedate
1   2012-01-01 a 2012-01-03
1   2012-01-02 a 2012-01-03
1   2012-01-03 a 2012-01-03
1   2012-01-04 b 2012-01-03
1   2012-01-05 b 2012-01-03
2   2013-01-01 a 2013-01-02
2   2013-01-02 a 2013-01-02
2   2013-01-03 b 2013-01-02
2   2013-01-04 b 2013-01-02

Is there a way to do this elegantly through plyr functions or even in base R?
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-17T10:31:52+00:00Added an answer on June 17, 2026 at 10:31 am

    Edit: As Sebastian mentions, I assume that the data.frame is ordered by the column date.

    One of the many solutions. Probably the tricky bit is to find the transition period. This can be accomplished with the help of rle.

    rle.df <- rle(df$state)
    # get indices of a-to-b transition -> 3,7
    idx <- cumsum(rle.df$lengths)[c(TRUE, FALSE)]
    # get indices of b-to-a transition -> 5,9
    idx2 <- cumsum(rle.df$lengths)[c(FALSE, TRUE)]
    # construct appropriate lengths -> 5,4
    idx2 <- c(idx2[1], diff(idx2))
    # do a rep with idx2 fro times and df$date[idx] for value
    df$changedate <- unlist(lapply(1:length(idx2), function(vv) {
        rep(df$date[idx[vv]], idx2[vv])
    }))
    
    > df
      id.      date. state changedate
    1   1 2012-01-01     a 2012-01-03
    2   1 2012-01-02     a 2012-01-03
    3   1 2012-01-03     a 2012-01-03
    4   1 2012-01-04     b 2012-01-03
    5   1 2012-01-05     b 2012-01-03
    6   2 2013-01-01     a 2013-01-02
    7   2 2013-01-02     a 2013-01-02
    8   2 2013-01-03     b 2013-01-02
    9   2 2013-01-04     b 2013-01-02
    

    Alternative solution using data.table (I just noticed that you also have a .id. column with which we can split and apply the date with the transition index found via rle).

    require(data.table)
    rle.df <- rle(df$state)
    idx  <- cumsum(rle.df$lengths)[c(TRUE, FALSE)]
    idx2 <- cumsum(rle.df$lengths)[c(FALSE, TRUE)]
    idx  <- c(idx[1], tail(idx, -1) - head(idx2, -1))
    
    dt <- data.table(df, key="id.")
    out <- dt[, `:=`(changedate=date.[idx[id.]]), by=id.]
    
    > out
        id.      date. state changedate
     1:   1 2012-01-01     a 2012-01-03
     2:   1 2012-01-02     a 2012-01-03
     3:   1 2012-01-03     a 2012-01-03
     4:   1 2012-01-04     b 2012-01-03
     5:   1 2012-01-05     b 2012-01-03
     6:   2 2013-01-01     a 2013-01-02
     7:   2 2013-01-02     a 2013-01-02
     8:   2 2013-01-03     b 2013-01-02
     9:   2 2013-01-04     b 2013-01-02
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following data frame: id<-c(1,2,3,4) date<-c(19970807,19970902,19971010,19970715) df<-data.frame(id,date) in which the type of
I have the following data frame. date id value 2012-01-01 1 0.3 2012-01-01 2
I have the following data: a=c(1:10) b=c(16:25) c=c(24:33) wa=c(3,7,3,3,3,3,3,3,3,1) wb=c(3,2,3,3,3,3,3,3,3,8) wc=c(4,1,4,4,4,4,4,4,4,1) z=data.frame(a,b,c,wa,wb,wc) I want
Let's say you have a data frame generated by the following commands: date <-
I have the following data frame: Date,Year,Austria,Germany,... 1969-12-31,1969,96.743,95.768,... 1970-01-30,1970,95.515,95.091,... 1970-02-27,1970,95.075,95.235,... Ultimately, I would like
I have the following data frame: id<-c(1,1,1,3,3,3) date<-c(23-01-07,27-01-07,30-01-07,11-12-07,12-12-07,01-01-08) df<-data.frame(id,date) df$date2<-as.Date(as.character(df$date), format = %d-%m-%y) id
I have a large data frame with date variables, which reflect first day of
I have the following data frame in R LOCATION COLOR STATE 1 A green
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
I have the following data frame: id <- c(1,1,2,3,3) date <- c(23-01-08,01-11-07,30-11-07,17-12-07,12-12-08) df <-

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.