Reshape data.frame with two columns into multiple columns with data (R)

拥有回忆 提交于 2019-12-10 15:21:32

问题


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


回答1:


You don't really have an "ID" variable, so you need to create one. It will be easier if Year was a character variable, so I've done that transformation below, in addition to adding an "ID" variable:

d <- within(d, {
  Year <- as.character(Year)
  ID <- ave(Year, Year, FUN=seq_along)
})

From here, it is easy to use dcast directly...

library(reshape2)
dcast(d, ID ~ Year, value.var="FQ")
#   ID  1975   1977  1979  1981  1983
# 1  1 3.156 10.304 4.729 4.856 9.887
# 2  2 8.980  7.861 7.216 3.438 3.850

... or reshape.

reshape(d, direction  = "wide", idvar="ID", timevar="Year")
#    ID FQ.1975 FQ.1977 FQ.1979 FQ.1981 FQ.1983
# 1   1   3.156  10.304   4.729   4.856   9.887
# 62  2   8.980   7.861   7.216   3.438   3.850


来源:https://stackoverflow.com/questions/20373797/reshape-data-frame-with-two-columns-into-multiple-columns-with-data-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!