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

In R use reshape package to use Names and Rownames as factors

这是我的数据:

             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 轴,行名作为不同的颜色或者也许方面取决于哪个看起来更好。任务是实际创建情节,所以我不会因为寻求帮助以我可以实际使用的方式获取数据而感到难过。

你可以用 base 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'),] 以获得您在问题中给出的确切两行 data.frame。

谢谢,我实际上是使用重塑得到它的:

给予:

 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

在基础 R 中,这本质上是一个 stacking 操作(使用来自@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