I searched many posts like this Subsetting a dataframe in R by multiple conditions about selecting the "which" function to select values dataframe dataframe and can not find another solution . The problem is as follows:
I have the following data set with thousands of cases:
> head(Datos)
tipo estacion hora usos
1 hábil A.SANIN X4 11
2 hábil ALAMOS X4 4
3 hábil AMANECER X4 45
4 hábil AMERICAS X4 2
5 hábil ATANASIO X4 10
6 hábil BELALCAZAR X4 5
. . . .
. . . .
. . . .
The variable to subset of dataframe above is "usos" The variable "tipo" takes the values : "hábil", "Sábado" and "Festivo". The variable "estacion" has 60 levels and the variable "hora" has 22 values: x4, x5, x6, ... x23 . As I need to calculate the quartiles according to all combinations of "tipo" , "estacion" and "hora" i use the "aggregate" function and calculate the critical values so I get this:
> head(todo)
Group.1 Group.2 Group.3 y1 y2
1 hábil X4 A.SANIN 1.5 21.5
2 Sábado X4 A.SANIN 4.0 12.0
3 Festivo X4 A.SANIN 0.0 0.0
4 hábil X5 A.SANIN 66.0 130.0
5 Sábado X5 A.SANIN 40.0 96.0
6 Festivo X5 A.SANIN 7.5 43.5
. .
. .
. .
Each row is a different case and the values y1 and y2 are my critical values. Need, according to the values y1 and y2 of the dataframe "todo" that I choose to values less than y1 or greater y2 of the variable "usos" from dataframe"Datos". But in a cycle, there are 3480 combinations on dataframe "todo", this is, 3480 rows. And store it in another Matrix.
For example, for the first case is as follows:
print(which(subset(Datos$usos,Datos$tipo=="hábil"&Datos$hora=="X4"&Datos$estacion=="A.SANIN")<todo$y1[1] | subset(Datos$usos,Datos$tipo=="hábil"&Datos$hora=="X4"&Datos$estacion=="A.SANIN")>todo$y2[1]))
I need to do that for all rows of the dataframe "all" and apply it to "use" the dataframe "Data".
THANK YOU!