Reputation: 1
My Matrix seems like this:
Actually, I have daily and hourly outputs for a week, which means I have 7 matrix for daily and 168 matrix for hourly.
My desired output is:
1: Getting the entire row from each matrix that third column has first value greater than 99.
For example:
[8,] 8, 3.5959768, 99.10497, 794880
2: Getting the entire row from each matrix that second column has first value less than 1.
For example:
[14,] 14, 0.7453416, 94.40994, 198720
3: There are two rows that are need to be captured from each matrix.
Finally, I need all of the rows that captured from all the matrix combine in one. So, for daily we should have 14 rows and for hourly we should have 336 rows.
Upvotes: 0
Views: 37
Reputation: 12640
Your question would be improved by having the data in text format (either as a copy of the matrix as printed, or the output from dput
).
In the absence of that, here's some sample data:
set.seed(123)
matrices <- replicate(7,
cbind(1:21, rnorm(21, 2, 1), rnorm(21, 99, 3), rnorm(21, 1e5, 1e5)),
simplify = FALSE)
You can do what you're asking as follows:
get_rows <- function(mat) {
rbind(mat[which(mat[, 3] > 99)[1], , drop = FALSE],
mat[which(mat[, 2] < 1)[1], , drop = FALSE])
}
desired_row_list <- lapply(matrices, get_rows)
desired_rows <- do.call("rbind", desired_row_list)
This subsets each matrix using the criteria specified, always just returning the first row to meet those criteria. It then joins them all together into a single matrix with rbind
.
desired_rows
# [,1] [,2] [,3] [,4]
# [1,] 6 3.71506499 101.51336 53334.46
# [2,] 8 0.73493877 95.58559 91663.09
# [3,] 2 0.92820877 99.99535 21509.55
# [4,] 1 0.98142462 98.33854 95497.23
# [5,] 1 2.23538657 101.06375 151686.20
# [6,] 9 -0.05324722 98.15881 103778.84
# [7,] 1 1.50070798 99.35774 90968.04
# [8,] 6 0.68919847 104.02709 81707.46
# [9,] 2 2.09049665 101.54893 106670.09
# [10,] 8 0.46709800 93.99757 24731.10
# [11,] 3 0.74135137 101.59734 357145.81
# [12,] 3 0.74135137 101.59734 357145.81
# [13,] 6 0.94498296 101.01209 54966.14
# [14,] 2 0.20971876 98.77933 128642.44
Upvotes: 1