1
votes

I have the following array which I called station:

A1 <- matrix(runif(120),24,5)
A1[1:12,1]<-2012
A1[13:24,1]<-2013
A1[1:12,2]<-(1:12)
A1[13:24,2]<-(1:12)
A1[1:12,3]<-seq(1,24,by=2)
A1[13:24,3]<-seq(1,24,by=2)

A2 <- matrix(runif(120),24,5)
A2[1:12,1]<-2012
A2[13:24,1]<-2013
A2[1:12,2]<-(1:12)
A2[13:24,2]<-(1:12)
A2[1:12,3]<-seq(1,24,by=2)
A2[13:24,3]<-seq(1,24,by=2)

station <- array(NA,c(24,5,2))
station[,,1] <- A1
station[,,2] <- A2
dimnames(station)[[2]]<-c('year','month','day','win_3','win_7')
dimnames(station)[[3]]<-c('station1','station2')
print(station)

I would like to extract max value of win_3 which I called Max_3Days through spring season (i.e, month 3,4 and 5) of each year for each station and specify the corresponding value of day and month (either 3,4 or 5).

The same thing for min value, I want to extract it from win_7 which I called Min_7Days through summer season (i.e, month 6,7 and 8) of each year for each station and specify the corresponding value of month (either 6,7 or 8) and day

I would like to keep the result in array format if it possible.

The result should be like this:

, , 1
                   Year Month day   Max_3Days   Year    Month  Day Min_7Days
  [1,]              2012     3   15      2800     2012      6    1       400
  [2,]              2013     4   2       2730     2013      6    4       100        

, , 2
                   Year Month day   Max_3Days   Year    Month  Day Min_7Days 
  [1,]              2012     4   15      2800     2012      7    10      250
  [2,]              2013     5   2       2750     2013      7    14      271        

I did specify spring and summer season and find the max, min values when I was having only one station as a data frame format, I want to do this for about 70 station in a (matrix) of an array format, and I want to keep the result in an array:

In case of data frame (only one station):

Summer<-station[which(station$month>"5"&station$month<"9"),]
Minima<-ddply(Summer, ~ year, summarise, month=month[which.min(win_7)],day=day[which.min(win_7)], Min_7Days =min(win_7, na.rm = TRUE))

Spring<-station[which(station$month>"2"&station$month<"6"),]
Maxima<-ddply(Spring, ~ year, summarise, month=month[which.max(win_3)],day=day[which.max(win_3)], Max_3Days =max(win_3, na.rm = TRUE))

Any suggestion would be appreciated!!

1
Please use dput to show the example dataset - akrun
I update my question, sorry for confusion - Nuha J. Alhowramy
@NuhaJ.Alhowramy could you re-confirm the expected output values shared? how did the max be in thousands? - joel.wilson
@joel.wilson: there is no problem with the max in thousands, but the month value is NA because each month in the actual data repeated 30 times (according to the no. of days) for example: month:1,1,1,1,1,1,1,1,1 then 2,2,2,2,2,2,2,3,3,3,3,3,3,.... and so on. I tried many things, nothing work for me:( - Nuha J. Alhowramy
@NuhaJ.Alhowramy that should not create a problem.. did you check my numbers? it's giving the results - joel.wilson

1 Answers

0
votes

currently i have made them into a list and went ahead.

l = vector('list', 2)
l[[1]] = data.frame(station[,,1])
l[[2]] = data.frame(station[,,2])

spring_end   <- 5
spring_start <- 3
summer_end   <- 8
summer_start <- 6

library(dplyr)
func <- function(df){

  df %>% group_by(year) %>% 
         summarise(   Max_3Days    = max(win_3[between(month, spring_start, spring_end)]),
                      Month_spring = month[between(month, spring_start, spring_end)][which.max(win_3[between(month, spring_start, spring_end)])],
                      Min_7Days    = min(win_7[between(month, summer_start, summer_end)]),
                      Month_summer = month[between(month, summer_start, summer_end)][which.min(win_7[between(month, summer_start, summer_end)])])
}

lapply(l, func)

#[[1]]
#   year Max_3Days Month_spring Min_7Days Month_summer
#1  2012 0.6521762            5 0.3547476            6
#2  2013 0.9627131            3 0.1754293            6

#[[2]]
#   year Max_3Days Month_spring  Min_7Days Month_summer
#1  2012 0.6115331            5 0.08505264            6
#2  2013 0.6051239            3 0.10938192            8