【问题标题】:In R use reshape package to use Names and Rownames as factors在 R 中使用 reshape 包来使用 Names 和 Rownames 作为因素
【发布时间】:2015-04-20 23:26:43
【问题描述】:

这是我的数据:

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

【问题讨论】:

  • 你能分享dput(yourdata)的结果吗?这将有助于创建可重现的数据版本。

标签: r dataframe reshape2 factors melt


【解决方案1】:

您可以使用基础 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' &amp; res$type%in%c('ON-ROAD','NON-ROAD'),] 以获取您在问题中给出的确切的两行 data.frame。

【讨论】:

    【解决方案2】:

    在基础 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
    

    【讨论】:

    • 感谢您的回答,我确实使用 reshape 得到了它。
    【解决方案3】:

    感谢我实际上是使用 reshape 得到的:

    通过给予:

     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
    

    【讨论】:

      猜你喜欢
      • 2013-12-25
      • 2012-06-19
      • 2015-01-22
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 2014-09-02
      相关资源
      最近更新 更多