mardi 30 mars 2021

How to write a loop for a dataframe with multiple conditions in R?

I have a dataframe with dates (2014-01-01 - 2021-02-04), hours (0 - 23) and electricity prices.

dataframe

With this data I need to go over each day, find highest and lowest prices and their maximum differences in that day and then collect the dates in which the differences are big enough. I also have another variable nr_hours (number of hours) which can be 1, 2, 3, 4 or 5. If nr_hours is 2, then I need to find 2 lowest and 2 highest prices and keep in mind that the lower prices need to be before higher prices. The first thing I did was to sort prices in group:

data <- data %>%
  group_by(Date) %>%
  arrange(Price, .by_group = TRUE)

I also wrote down the logic on how the loop should work:

nr_hours = 2  #1, 2, 3, 4, 5
i = 1  #max price row number
j = 24  #min price row number
k = 0  #number of selected hours' prices

#for each date in data$Date:
#-if hours are in descending order and k != nr_hours:
#--TRUE: break
#--FALSE: if k == nr_hours:
#---TRUE: next date
#---FALSE: if i row hour <= (max hour - 1):
#----FALSE: i += 1 and move back to the first if
#----TRUE: if j row hour >= (min hour + 1):
#-----FALSE: j -= 1 and move back to the first if
#-----TRUE: if i row hour < j row hour:
#------TRUE: choose i and j row prices, remove these rows and move back to the first if with k += 1, i = 1 and j = 24-2k
#------FALSE: if (i+1) and i row price difference < j and (j-1) row difference:
#-------TRUE: i += 1 and move back to the first if
#-------FALSE: if (i+1) row price < (j-1) row price:
#--------TRUE: j -= 1 and move back to the first if
#--------FALSE: i += 1 and j = 24-2k and move back to the first if

How to write this in R?

Aucun commentaire:

Enregistrer un commentaire