A trivial question but I cant find the answer as of yet.
I want to split the dataframe column 'year' into a set of new columns with each year the column name and subsequent data below it:
Year FQ
1975 3.156
1975 8.980
1977 10.304
1977 7.861
1979 4.729
1979 7.216
1981 4.856
1981 3.438
1983 9.887
1983 3.850
desired output:
1975 1977 1979 1981 1983
3.156 10.304 4.729 4.856 9.887
8.980 7.861 7.216 3.438 3.850
sample data:
d<-structure(list(Year = structure(1:10, .Label = c("1975", "1975",
"1977", "1977", "1979", "1979", "1981", "1981", "1983", "1983",
"1985", "1985", "1987", "1987", "1988", "1988", "1991", "1991",
"1993", "1993", "1995", "1995", "1997", "1997", "2000", "2000",
"2001", "2001", "2003", "2003", "2005", "2005", "2007", "2007",
"2009", "2009", "2011", "2011"), class = "factor"), FQ = c(3.156,
8.98, 10.304, 7.861, 4.729, 7.216, 4.856, 3.438, 9.887, 3.85)), .Names = c("Year",
"FQ"), class = "data.frame", row.names = c(1L, 62L, 123L, 184L,
245L, 306L, 367L, 428L, 489L, 550L))
I have tried melting the data:
melt(d, id.vars = "Year")
and then using cast:
cast(d, Year~value)
and reshape
d1<-reshape(d, idvar="Year", timevar="FQ", direction="wide")
but to no avail