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0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 
0, 0, 3, 0, 0, 1, 0, 0, 2, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 0, 
0, 0, 0, 1, 0, 0, 2, 0, 1, 2, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 
0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 3, 1, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 1, 1, 0, 0, 1, 
0, 0, 0), .Dim = c(22L, 20L), .Dimnames = list(NULL, c("month", 
"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", 
"", "", "")))

and :

month <- c(1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2)

I want to sum by row, so I used this code :

su_test <- list()
for (i in 1:ncol(TEST)){
    su_test[[i]] <- tapply(TEST[,i], month, sum)
}

su_test <- do.call(cbind, su_test)

and to check the quantile :

su_test_obs <- apply(su_test,1,quantile,c(0.1,0.9))

This is an observation simulation per month. However I also have the detail by area. :

TEST2 <- structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 
1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 3, 1, 0, 
0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 1, 2, 0, 0, 1, 0, 0, 1, 1,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"", "", "", "", "", 
"", "", "", "")))

area <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

I would like to have the same result of su_test_obs but with the details of the areas in addition like list(month, area) but I don't understand the logic. Would you have a solution please? Maybe there is a solution simpler with dplyr?

Thanks

Upvotes: 0

Views: 33

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388817

It would be much simpler if you convert your matrix to dataframe. We can then use aggregate which can be applied to multiple groups easily

df <- data.frame(TEST2)
apply(aggregate(.~month + area, df, sum)[-c(1, 2)], 1, quantile, c(0.1,0.9))

#    [,1] [,2]
#10%    2  1.7
#90%    6  5.3

Upvotes: 1

Related Questions