Adding data points in a column by factors in R -


the data.frame my_data consists of 2 columns("pm2.5" & "years") & around 6400000 rows. data.frame has various data points pollutant levels of "pm2.5" years 1999, 2002, 2005 & 2008. have done data.drame:

{  my_data <- arrange(my_data,year)  my_data$year <- as.factor(my_data$year) my_data$pm2.5 <- as.numeric(my_data$pm2.5) } 

i want find sum of pm2.5 levels (i.e sum of data points under pm2.5) according different year. how can it.

!the image shows first 20 rows of data.frame. since column "years" arranged, showing 1999

say data:

library(plyr) # <- don't forget tell libraries using 

give easy sample set

my_data <- data.frame(year=sample(c("1999","2002","2005","2008"), 10, replace=t), pm2.5 = rnorm(10,mean = 5))  my_data <- arrange(my_data,year)  my_data$year <- as.factor(my_data$year) my_data$pm2.5 <- as.numeric(my_data$pm2.5)  > my_data    year    pm2.5 1  1999 5.556852 2  2002 5.508820 3  2002 4.836500 4  2002 3.766266 5  2005 6.688936 6  2005 5.025600 7  2005 4.041670 8  2005 4.614784 9  2005 4.352046 10 2008 6.378134 

one way (out of many, many ways shown simple google search):

> with(my_data, (aggregate(pm2.5, by=list(year), fun="sum")))   group.1         x 1    1999  5.556852 2    2002 14.111586 3    2005 24.723037 4    2008  6.378134 

Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -