Expand ranges defined by “from” and “to” columns

前端 未结 9 1902
悲哀的现实
悲哀的现实 2020-11-22 07:02

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

9条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 07:23

    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")]
    

提交回复
热议问题