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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:32:35+00:00 2026-06-05T15:32:35+00:00

I have a state column in a dataframe and I want to create two

  • 0

I have a state column in a dataframe and I want to create two new columns:
One that looks ahead to the next stage change and one that looks back to the previous state change. So the resulting dataframe will look like below:

state coming  previous
  a     a-b     NA
  a     a-b     NA
  a     a-b     NA
  a     a-b     NA
  b     b-c     a-b
  b     b-c     a-b
  b     b-c     a-b
  c     c-a     b-c
  c     c-a     b-c
  c     c-a     b-c
  a     NA      c-a
  a     NA      c-a

Or maybe even better, but now you just create two transition columns:

state trans1   trans2
  a     a-b     NA
  a     a-b     NA
  a     a-b     NA
  a     a-b     NA
  b     a-b     b-c 
  b     a-b     b-c
  b     a-b     b-c
  c     c-a     b-c
  c     c-a     b-c
  c     c-a     b-c
  a     c-a     NA 
  a     c-a     NA

[Edit]
changed states named “1” to “c” because it was confusing

  • 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-05T15:32:36+00:00Added an answer on June 5, 2026 at 3:32 pm

    Let’s give that dataframe a name, say ‘inp’. Use the rle function to construct the sequence of “states”:

    > rle(inp$state)
    Run Length Encoding
      lengths: int [1:4] 4 3 3 2
      values : chr [1:4] "a" "b" "1" "a"
    
    runinp <- rle(inp$state)$values
    paste( runinp[-length(runinp)], runinp[-1], sep="-")
    # [1] "a-b" "b-1" "1-a"
    inp$coming <- rep( c( paste( runinp[-length(runinp)], runinp[-1], sep="-"), NA), 
                        rle(inp$state)$lengths )
    inp$coming
    # [1] "a-b" "a-b" "a-b" "a-b" "b-1" "b-1" "b-1" "1-a" "1-a" "1-a" NA    NA   
    
    inp$previous <- 
     rep( c( NA_character_, paste(runinp[-1], runinp[-length(runinp)], sep="-")), 
          rle(inp$state)$lengths )
    inp$previous
     [1] NA    NA    NA    NA    "b-a" "b-a" "b-a" "1-b" "1-b" "1-b" "a-1" "a-1"
    

    (I was able to overcome my difficulty with understanding your first request, but had persistent difficulty with the second part.)

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

Sidebar

Related Questions

I have a csv file that contains a bunch of states in one column
I have three columns and the default visible state of last column is false.My
I have a dataframe with several columns: state county year Then x, y, and
I have a column that contains city, state and zip code. CITY_STATE_ZIP -------------------------------------------------- Monroe,
I have a table that stores multiple items for a state and I want
I have 3 columns in my table X: Id State Type 1 NJ Form1
I have a table column where each row has one of three states, and
Lets say I have a table with columns such as: ID Name City State
I have state data in a txt file that I used to seed my
basically i have (state, state code) pairs, that are subsets of country [USA] ->

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.