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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T15:59:57+00:00 2026-06-07T15:59:57+00:00

This problem is also known as ‘transforming a "start-end" dataset to a panel dataset’

  • 0

This problem is also known as ‘transforming a "start-end" dataset to a panel dataset’

I have a data frame containing "name" of U.S. Presidents, the years when they start and end in office, ("from" and "to" columns). Here is a sample:

presidents <- data.frame(
  name = c("Bill Clinton", "George W. Bush", "Barack Obama"),
  from = c(1993, 2001, 2009),
  to = c(2001, 2009, 2012)
)
presidents
#>             name from   to
#> 1   Bill Clinton 1993 2001
#> 2 George W. Bush 2001 2009
#> 3   Barack Obama 2009 2012

I want to create data frame with two columns ("name" and "year"), with a row for each year that a president was in office. Thus, I need to create a regular sequence with each year from "from", to "to". Here’s my expected out:

name           year
Bill Clinton   1993
Bill Clinton   1994
...
Bill Clinton   2000
Bill Clinton   2001
George W. Bush 2001
George W. Bush 2002
... 
George W. Bush 2008
George W. Bush 2009
Barack Obama   2009
Barack Obama   2010
Barack Obama   2011
Barack Obama   2012

I know that I can use data.frame(name = "Bill Clinton", year = seq(1993, 2001)) to expand things for a single president, but I can’t figure out how to iterate for each president.

How do I do this? I feel that I should know this, but I’m drawing a blank.

Update 1

OK, I’ve tried both solutions, and I’m getting an error:

foo<-structure(list(name = c("Grover Cleveland", "Benjamin Harrison", "Grover Cleveland"), from = c(1885, 1889, 1893), to = c(1889, 1893, 1897)), .Names = c("name", "from", "to"), row.names = 22:24, class = "data.frame")
ddply(foo, "name", summarise, year = seq(from, to))
Error in seq.default(from, to) : 'from' must be of length 1
  • 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-07T16:00:00+00:00Added an answer on June 7, 2026 at 4:00 pm

    You can use the plyr package:

    library(plyr)
    ddply(presidents, "name", summarise, year = seq(from, to))
    #              name year
    # 1    Barack Obama 2009
    # 2    Barack Obama 2010
    # 3    Barack Obama 2011
    # 4    Barack Obama 2012
    # 5    Bill Clinton 1993
    # 6    Bill Clinton 1994
    # [...]
    

    and if it is important that the data be sorted by year, you can use the arrange function:

    df <- ddply(presidents, "name", summarise, year = seq(from, to))
    arrange(df, df$year)
    #              name year
    # 1    Bill Clinton 1993
    # 2    Bill Clinton 1994
    # 3    Bill Clinton 1995
    # [...]
    # 21   Barack Obama 2011
    # 22   Barack Obama 2012
    

    Edit 1: Following’s @edgester’s “Update 1”, a more appropriate approach is to use adply to account for presidents with non-consecutive terms:

    adply(foo, 1, summarise, year = seq(from, to))[c("name", "year")]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a problem am facing for long now also asked a question regarding
This problem has bugged me so many times and i have now decided to
There is this well-known problem that browsers don't put Ajax request in the request
I have two sets of data in this form: x | y | z
This problem is same as asked in here . Given a list of coins,
This problem seems very simple to me, but I've been unable to fix it,
This problem is caused by my acknowledge of English and jQuery or maybe something
This problem has been bugging me for a long time. For example, the code
This problem is about concurrent high speed inserts. I must admit it is very
This problem is very strange and I'm hoping someone can help me. For the

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.