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

前端 未结 9 1863
悲哀的现实
悲哀的现实 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条回答
  •  萌比男神i
    2020-11-22 07:20

    Here is a quick base-R solution, where Df is your data.frame:

    do.call(rbind, apply(Df, 1, function(x) {
      data.frame(name=x[1], year=seq(x[2], x[3]))}))
    

    It gives some warnings about row names, but appears to return the correct data.frame.

提交回复
热议问题