Reputation: 889
The data I have is for 8 variables named s1
, s2
... s8
for 122 days (which goes from 152
till 273 day of the year) and for 11 years (from 1997 till 2007). Here is the data:
#generate sample data
set.seed(1)
var<-as.data.frame(cbind(runif(1342),runif(1342),runif(1342),runif(1342),runif(1342),runif(1342),runif(1342),runif(1342)))
names(var)<-c("s1","s2","s3","s4","s5","s6","s7","s8")
day<-rep(152:273,times=11)
year<-rep(1997:2007,each=122)
dat<-as.data.frame(cbind(day,year,var))
What I want to do is this:
1) Select the first variable s1
2) For the year 1997, add the value of s1
of the 122 days
3) Do this for all the years till 2007
4) Select the second variable s2
and repeat
Ideally I want to save the outputs in the 3rd column of the following matrix
#create a matrix to store data
mat<-matrix(nrow=88,ncol=3)
var<-c("s1","s2","s3","s4","s5","s6","s7","s8")
col1<-rep(var,each=11)
col2<-rep(1997:2007,times=8)
mat[, 1:2] <- cbind(col1, col2)
For e.g. after summing the 122 days of s1 for 1997, the sum should be stored in mat[1,3]
. And so on. I created the following loop to do this
#create a loop
for (i in 3:10){
dat1<-dat[,c(1:2,i)] #selects the s1 data
for (j in 1997:2007){
year<-dat1[dat1$year==j,] # selects the data of 1997 from s1
total<-sum(year[,3]) #sums up the 122 days of s1 for the year 1997
mat<-total?????????????????
}}
Now I am stuck in the last part. How do I assign total
to mat[1,3]
and the next total to mat[1,4]
and so on.
Thank you.
Upvotes: 0
Views: 122
Reputation: 680
The dplyr option of user2100721 is better than the following loop but if you wanted to know how to do exactly what you stated:
for (i in 3:10){
dat1<-dat[,c(1:2,i)] #selects the s1 data
for (j in 1997:2007){
year <- dat1[dat1$year==j,] # selects the data of 1997 from s1
total <- sum(year[,3]) #sums up the 122 days of s1 for the year 1997
mat[mat[,1] == names(dat)[i] & mat[, 2] == j, 3] <- total
}}
Basically, you can assign to a specific cell of the matrix if you want. I select here the line by matching the line to the correct column (names(dat)[i])
and the correct year.
Upvotes: 1
Reputation: 3587
Use dplyr
package
library(dplyr)
dat %>% group_by(year) %>% summarise_each(funs(sum))
Upvotes: 0