Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Function to identify first positive observation by year

Tags:

r

I need to write a function in R to return the first date in a series for which the value of a column is greater than 0. I would like to identify that date for each year in the dataframe.

For example, given this example data...

Date         Year     Catch

3/12/2001    2001     0
3/19/2001    2001     7
3/24/2001    2001     9
4/6/2002     2002     12 
4/9/2002     2002     0
4/15/2002    2002     5
4/27/2002    2002     0
3/18/2003    2003     0
3/22/2003    2003     0
3/27/2003    2003     15

I would like R to return the first date for each year with catch > 0

Year    Date 

2001    3/19/2001
2002    4/6/2002
2003    3/27/2003

I had been working with the min function below, but it only returns the line number and I was unable to return a value for each year in the dataframe. min(which(data$Catch > 0))

I'm new to writing my own functions in R. Any help would be appreciated. Thanks.

like image 389
rjb1990 Avatar asked Nov 19 '25 11:11

rjb1990


1 Answers

library(dplyr)

df1 %>% 
  group_by(Year) %>% 
  slice(which.max(Catch > 0))

# # A tibble: 3 x 3
# # Groups:   Year [3]
#   Date        Year Catch
#   <date>     <int> <int>
# 1 2001-03-19  2001     7
# 2 2002-04-06  2002    12
# 3 2003-03-27  2003    15

Data:

df1 <-
structure(list(Date = structure(c(11393, 11400, 11405, 11783, 
11786, 11792, 11804, 12129, 12133, 12138), class = "Date"), Year = c(2001L, 
2001L, 2001L, 2002L, 2002L, 2002L, 2002L, 2003L, 2003L, 2003L
), Catch = c(0L, 7L, 9L, 12L, 0L, 5L, 0L, 0L, 0L, 15L)), .Names = c("Date", 
"Year", "Catch"), row.names = c(NA, -10L), class = "data.frame")
like image 77
IceCreamToucan Avatar answered Nov 21 '25 00:11

IceCreamToucan