I have a data frame containing the "name" of the US presidents, the years when they start and end in the office (columns "from" and "to" ). Here is an example:
name from to Bill Clinton 1993 2001 George W. Bush 2001 2009 Barack Obama 2009 2012
... and output from 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")
I want to create a data frame with two columns ( "name" and "year" ), with each row for each year when the president was in office. So I need to create a regular sequence every year from " from " to "to" . Here I was expecting:
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
I know that I can use data.frame(name = "Bill Clinton", year = seq(1993, 2001)) to expand opportunities for one president, but I cannot understand how iterations for each president.
How can I do it? I feel like I should know, but I draw a space.
Update 1
Ok, I tried both solutions and I get an error:
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