【发布时间】:2022-11-23 02:18:48
【问题描述】:
我有一个数据框,其中包含美国总统的"name",以及他们上任和离任的年份("from" 和"to" 列)。这是一个示例:
name from to
Bill Clinton 1993 2001
George W. Bush 2001 2009
Barack Obama 2009 2012
...以及dput的输出:
dput(tail(presidents, 3))
structure(list(name = c("Bill Clinton", "George W. Bush", "Barack Obama"
), from = c(1993, 2001, 2009), to = c(2001, 2009, 2012)), .Names = c("name",
"from", "to"), row.names = 42:44, class = "data.frame")
我想创建包含两列("name" 和"year")的数据框,总统在任的每一年都有一行。因此,我需要创建一个每年从“from”到"to" 的常规序列。这是我的预期:
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
我知道我可以使用 data.frame(name = "Bill Clinton", year = seq(1993, 2001)) 为单个总统扩展内容,但我不知道如何为每个总统进行迭代。
我该怎么做呢?我觉得我应该知道这一点,但我一片空白。
更新 1
好的,我已经尝试了两种解决方案,但出现错误:
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
【问题讨论】: