简体   繁体   中英

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

Here is my data:

             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

It's of type data.frame. What I would like to do is use the reshape package to make it look like this:

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

This is a part of a homework assignment in which I have to use the ggplot package to create a plot that uses the years as the x axis and the emissions data as my y axis and the rownames as different colors or maybe facets depending on which looks better. The assignment is to actually create the plot so I don't feel to bad about asking for help to get the data in a way I can actually use it.

In base R, this is essentially a stack ing operation (using df from @bgoldst's answer):

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

You can do this with 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

If you want a specific subset of the data, you can index for it afterward, eg store the result in res and then get res[res$year=='1999' & res$type%in%c('ON-ROAD','NON-ROAD'),] to get the exact two-row data.frame you've given in your question.

Thanks I actually got it using reshape:

by giving:

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM