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

前端 未结 9 1719
悲哀的现实
悲哀的现实 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:31

    Use by to create a by list L of data.frames, one data.frame per president, and then rbind them together. No packages are used.

    L <- by(presidents, presidents$name, with, data.frame(name, year = from:to))
    do.call("rbind", setNames(L, NULL))
    

    If you don't mind row names then the last line could be reduced to just:

    do.call("rbind", L)
    

提交回复
热议问题