I have got a matrix, that I generated using tapply.
It looks like this:
NON-ROAD NONPOINT ON-ROAD POINT
1999 522.94000 2107.625 346.82000 296.7950
2002 240.84692 1509.500 134.30882 569.2600
2005 248.93369 1509.500 130.43038 1202.4900
2008 55.82356 1373.207 88.27546 344.9752
as you can see that I don't have any id variable that I can use to melt all the 4 columns into a single column.
The dataset is around 30MB in size : data
to generate the matrix:
NEI <- readRDS("summarySCC_PM25.rds")
data <- with(NEI[NEI$fips=="24510",],tapply(Emissions,list(year,type),sum))
> class(data)
[1] "matrix"
Expected Output:
Year Type Emission
1 1999 NON-ROAD 522.94000
2 2002 NON-ROAD 240.84692
3 2005 NON-ROAD 248.93369
4 2008 NON-ROAD 55.82356
5 1999 NONPOINT 2107.62500
6 2002 NONPOINT 1509.50000
7 2005 NONPOINT 1509.50000
8 2008 NONPOINT 1373.20731
9 1999 ON-ROAD 346.82000
10 2002 ON-ROAD 134.30882
11 2005 ON-ROAD 130.43038
12 2008 ON-ROAD 88.27546
13 1999 POINT 296.79500
14 2002 POINT 569.26000
15 2005 POINT 1202.49000
16 2008 POINT 344.97518
Also, I have seen solutions that can directly convert the original dataset into my expected output using aggregate
. But i can't use that function to maintain a uniformity with my other answers, in my assignment.
It would also be great if the solution is done by native R functions, if possible.