Reputation: 1
How do I find the first minimum value in one column of a dataframe and output a new dataframe with just that row?
For example, for a dataframe named "hospital", for each node, I want to find the minimum time at which "H" is >=1.
node | time | H |
---|---|---|
1 | 1 | 0 |
2 | 1 | 0 |
3 | 1 | 0 |
1 | 2 | 0 |
2 | 2 | 0 |
3 | 2 | 2 |
1 | 3 | 0 |
2 | 3 | 1 |
3 | 3 | 2 |
1 | 4 | 1 |
2 | 4 | 4 |
3 | 4 | 0 |
The result I want to be able to output is:
node | time | H |
---|---|---|
1 | 4 | 1 |
2 | 3 | 1 |
3 | 2 | 2 |
Upvotes: 0
Views: 128
Reputation: 52359
One way is to filter your dataframe, and then take the first minimum element for each group:
library(dplyr)
df %>%
filter(H > 0) %>%
group_by(node) %>%
slice_min(time, n = 1)
node time H
<int> <int> <int>
1 1 4 1
2 2 3 1
3 3 2 2
Upvotes: 1