I'm looking to find the max values of different columns based on specified rows of each column.
My actual data frame is 50K columns and 1K+ rows so I can't use a loop without greatly increasing run time.
Data Frame:
row | V1 | V2 | V3 | V4 |
---|---|---|---|---|
1 | 5 | 2 | 4 | 5 |
2 | 3 | 5 | 1 | 6 |
3 | 7 | 3 | 2 | 6 |
4 | 2 | 5 | 3 | 10 |
5 | 6 | 9 | 1 | 2 |
beg_row <- c(2, 1, 2, 3)
end_row <- c(4, 3, 3, 5)
output:
c(7, 5, 2, 10)
You can try mapply
(but I suspect that it won't speed up the runtime if you have massive columns)
> mapply(function(x, y, z) max(x[y:z]), df[-1], beg_row, end_row)
V1 V2 V3 V4
7 5 2 10
Data
df <- structure(list(row = 1:5, V1 = c(5L, 3L, 7L, 2L, 6L), V2 = c(
2L,
5L, 3L, 5L, 9L
), V3 = c(4L, 1L, 2L, 3L, 1L), V4 = c(
5L, 6L, 6L,
10L, 2L
)), class = "data.frame", row.names = c(NA, -5L))
beg_row <- c(2, 1, 2, 3)
end_row <- c(4, 3, 3, 5)