I have the following matrix M
structure(c(0, 0.2, 0.4, 0.6, 0.8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0.4, 0.6, 0.8, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 423, 176, 135,
30, 4), .Dim = c(5L, 19L), .Dimnames = list(NULL, c("pregnant_min",
"glucose_min", "blood_min", "skin_min", "INSULIN_min", "MASS_min",
"DIAB_min", "AGE_min", "CLASS_min", "pregnant_max", "glucose_max",
"blood_max", "skin_max", "INSULIN_max", "MASS_max", "DIAB_max",
"AGE_max", "CLASS_max", "NumOfObser")))
and a list L
:
L = list(1L, 2L, 3:5)
The elements of the list indicate which rows of M
should be grouped together.
The first and second lines should be groups of their own. The 3-5 lines should form a group in the following sense:
Rows 3-5 of M should be replaced with one row, whose min
of every value should be the min
of the minimum of rows 3-5, the max
should be the maximum, and its number of observations should be the sum.
So the output should look like this:
structure(c(0, 0.2, 0.4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.2, 0.4, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 423,
176, 169), .Dim = c(3L, 19L), .Dimnames = list(NULL, c("pregnant_min",
"glucose_min", "blood_min", "skin_min", "INSULIN_min", "MASS_min",
"DIAB_min", "AGE_min", "CLASS_min", "pregnant_max", "glucose_max",
"blood_max", "skin_max", "INSULIN_max", "MASS_max", "DIAB_max",
"AGE_max", "CLASS_max", "NumOfObser")))
The elements of the list L
can be composed of any combination of 1-5, corresponding to the number of rows of M
.
How can I achieve this output in the general case? So far I have looped through the elements of L
, but I am pretty sure there is a more neat/efficient way of doing this.