我有一个包含"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