Count consecutive number of days with condition in R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Count consecutive number of days with condition in R

bhimchaulagain
My question is about calculating the days with consecutive hours that meet
the certain conditions. For example, I have a data frame which is a hourly
weather data as mentioned below. I am trying to calculate the number of days
within a specified period (for eg Start date: 11/15/13 and End date:
11/19/13) having 2m T avg (F) in between 65-75 & 'RelHum avg 2m (pct)' >=90
consecutive for 4 or more than 4 hours in each day. Both temperature and
relative humidity conditions mentioned above should satisfy the condition of
consecutive for 4 or more than 4 hours. I couldn't even start to work on
this problem. I would greatly appreciate if you can help me in this problem.

Period     2m T avg (F) RelHum avg 2m (pct)
------     -----------  ------ ------------
11/15/13 0:00   57.91   93
11/15/13 1:00   57.93   93
11/15/13 2:00   58.8    92
11/15/13 3:00   58.99   92
11/15/13 4:00   58.79   93
11/15/13 5:00   59.56   94
11/15/13 6:00   59.82   94
11/15/13 7:00   61.39   95
11/15/13 8:00   66.56   92
11/15/13 9:00   72.93   82
11/15/13 10:00 76.79    72
11/15/13 11:00 77.82    70
11/15/13 12:00 77.99    70
11/15/13 13:00 78.69    68
11/15/13 14:00 77.66    70
11/15/13 15:00 76.94    70
11/15/13 16:00 76.53    70
11/15/13 17:00 74.7     76
11/15/13 18:00 72.96    81
11/15/13 19:00 71.63    84
11/15/13 20:00 70.79    87
11/15/13 21:00 70.33    88
11/15/13 22:00 68.49    90
11/15/13 23:00 67.86    92
11/16/13 0:00  68.81    92
11/16/13 1:00  69.3     91
11/16/13 2:00  69.07    92
11/16/13 3:00  69.35    92
11/16/13 4:00  69.33    93
11/16/13 5:00  69.3     94
11/16/13 6:00  69.04    95
11/16/13 7:00  69.08    95
11/16/13 8:00  70.73    95
11/16/13 9:00  72.86    94
11/16/13 10:00 75.15    93
11/16/13 11:00 76.09    89

I have written some codes to calculate the number of hours in a certain time
period having the temperature and RH conditions met. However, I got problems
while trying to calculate the number of days in a specified period with
temperature and RH conditions as mentioned above which is continuous for 4
or more than 4 hours in each day. I used the following code to calculate the
number of hours for the temperature and Rh conditions which will not
consider if those conditions are consecutive for 4 or more than 4 hours.

fawn <- read_excel('FAWN_report.xlsx')
fawn <-as.data.frame(fawn)

#Visualize data (if needed)
head(fawn)

fawn$days <- floor_date(fawn$Period, "day")
fawn$weeks <- floor_date(fawn$Period, 'week')
fawn$month <- floor_date(fawn$Period, 'month')
fawn$hours <- floor_date(fawn$Period+1, 'hours')

#this function returns the hour count between two dates satisfying
#specific conditions. Enter start date, end date, Relative
#humidity (inclusive), average Temperature low and high(inclusive) if needed
numberOfHours <- function(start_date, end_date, start_time, end_time, RH,
avgTempLow=NULL, avgTempHigh = NULL){
  fawnSubset <- fawn %>%
    subset(days > start_date & days <= end_date & `RelHum avg 2m (pct)` >=
RH)
 
 
 
  if(start_time > 24 || start_time < 0 || end_time > 24 || end_time < 0){
    stop('Please choose time that is from 0-24')
  }
 
  if(start_time > end_time){
    fawnTime1 <- subset(fawnSubset, hour(fawnSubset$hours) >= start_time)
    fawnTime2 <- subset(fawnSubset, hour(fawnSubset$hours) <= end_time)
    fawnSubset <- rbind(fawnTime1, fawnTime2)
  } else {
    fawnSubset <- subset(fawnSubset, hour(fawnSubset$hours) >= start_time &
hour(fawnSubset$hours) <= end_time)
  }
 
  if(is.null(avgTempLow)==TRUE){
    timePeriod <- paste(start_date,' to ', end_date)
    tally <- dim(fawnSubset)[1]
    answer <- cbind(timePeriod, tally)
  } else {
    fawnSubset <- fawnSubset %>%
      subset(`2m T avg (F)` >= avgTempLow & `2m T avg (F)`<= avgTempHigh)
   
    timePeriod <- paste(start_date,' to ', end_date)
    tally <- dim(fawnSubset)[1]
    answer <- cbind(timePeriod, tally)
  }
 
  return(answer)
}  


write.table(numberOfHours('2013-12-20', '2014-01-09', 0, 24, 90, 65, 75),
sep = ",", col.names = T, row.names = F,
'2014_numberOfHours_specifiedHours__RH_T_7-27.csv')



--
Sent from: http://r.789695.n4.nabble.com/datatable-help-f2315188.html
_______________________________________________
datatable-help mailing list
[hidden email]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help