在R中使用reshape包来使用Names和Rownames作为因子

时间:2015-04-20 23:26:44

标签: r dataframe reshape2 factors melt

这是我的数据:

             1999      2002      2005       2008
NON-ROAD  522.940  240.8469  248.9337   55.82356
NONPOINT 2107.625 1509.5000 1509.5000 1373.20731
ON-ROAD   346.820  134.3088  130.4304   88.27546
POINT     296.795  569.2600 1202.4900  344.97518

类型为data.frame。我想要做的是使用reshape包使它看起来像这样:

year  type       Emissions
1999 ON-Road    346.820
1999 NON-Road   522.940

这是家庭作业的一部分,我必须使用ggplot包来创建一个图,该图使用年份作为x轴,排放数据作为我的y轴,rownames作为不同颜色或可能取决于面看起来更好。这个任务是实际创建情节,所以我不想要求帮助以实际使用它的方式获取数据。

3 个答案:

答案 0 :(得分:1)

在基础R中,这实际上是一个stack操作(使用来自@ bgoldst答案的df):

data.frame(type=rownames(df),setNames(stack(df),c("emissions","year")))

#       type  emissions year
#1  NON-ROAD  522.94000 1999
#2  NONPOINT 2107.62500 1999
#3   ON-ROAD  346.82000 1999
#4     POINT  296.79500 1999
#5  NON-ROAD  240.84690 2002
#6  NONPOINT 1509.50000 2002
#7   ON-ROAD  134.30880 2002
#8     POINT  569.26000 2002
#9  NON-ROAD  248.93370 2005
#10 NONPOINT 1509.50000 2005
#11  ON-ROAD  130.43040 2005
#12    POINT 1202.49000 2005
#13 NON-ROAD   55.82356 2008
#14 NONPOINT 1373.20731 2008
#15  ON-ROAD   88.27546 2008
#16    POINT  344.97518 2008

答案 1 :(得分:0)

您可以使用基础R执行此操作:

df <- data.frame(c(522.940,2107.625,346.820,296.795), c(240.8469,1509.5000,134.3088,569.2600), c(248.9337,1509.5000,130.4304,1202.4900), c(55.82356,1373.20731,88.27546,344.97518), row.names=c('NON-ROAD','NONPOINT','ON-ROAD','POINT') ); names(df) <- c('1999','2002','2005','2008');
do.call(rbind,lapply(names(df), function(n) data.frame(year=n, type=rownames(df), Emissions=df[[n]] ) ));
##    year     type  Emissions
## 1  1999 NON-ROAD  522.94000
## 2  1999 NONPOINT 2107.62500
## 3  1999  ON-ROAD  346.82000
## 4  1999    POINT  296.79500
## 5  2002 NON-ROAD  240.84690
## 6  2002 NONPOINT 1509.50000
## 7  2002  ON-ROAD  134.30880
## 8  2002    POINT  569.26000
## 9  2005 NON-ROAD  248.93370
## 10 2005 NONPOINT 1509.50000
## 11 2005  ON-ROAD  130.43040
## 12 2005    POINT 1202.49000
## 13 2008 NON-ROAD   55.82356
## 14 2008 NONPOINT 1373.20731
## 15 2008  ON-ROAD   88.27546
## 16 2008    POINT  344.97518

如果您需要特定的数据子集,可以在以后对其进行索引,例如:将结果存储在res中,然后获取res[res$year=='1999' & res$type%in%c('ON-ROAD','NON-ROAD'),]以获得您在问题中给出的确切的两行数据。

答案 2 :(得分:0)

谢谢我实际上使用了重塑:

给予:

 df$type=rownames(df)#creates a column from rownames of types
  df2<-melt(datasum,variable="year")#gives long form data with year and types as columns