-3

I've multiple stations (+1000) with more than 50 years to work on it, so I configured my df with the stations on the columns and dates on rows as the example. Now I need to make summations of my parameter for each year in each column of data, but also i must know how many fields with no NA it counted to give me a specific value for each year in each station. I hope you guys can help me, and sorry about the syntax and language.

year<-c(rep(2000,12),rep(2001,12),rep(2002,12), rep(2003,12))
data <- data.frame( year, month=rep(1:12,4),est1=rnorm(12*4,2,1),est2=rnorm(12*4,2,1),est3=rnorm(12*4,2,1))
data[3,3]<-NA 
lebelinoz
  • 4,380
  • 9
  • 27
  • 52

1 Answers1

1

Sums:

> apply(data[,-(1:2)], 2, tapply, data$year, sum, na.rm=T)
         est1     est2     est3
2000 23.46997 21.36984 28.24381
2001 27.32517 28.84098 24.11784
2002 23.41737 25.47548 23.82606
2003 24.63551 24.51148 28.17723

Non NA's:

> apply(!is.na(data[,-(1:2)]), 2, tapply, data$year, sum)
     est1 est2 est3
2000   11   12   12
2001   12   12   12
2002   12   12   12
2003   12   12   12

And a version without apply (see @r2evans comment below):

sapply(data[,-(1:2)], tapply, data$year, sum, na.rm=T)

sapply(data.frame(!is.na(data[,3:5])), tapply, data$year, sum)
Łukasz Deryło
  • 1,458
  • 1
  • 10
  • 26