Search code examples
reconomics

Extract rows for the first occurrence of a variable in a data frame when date is on the x axis and states


Edited: Example:

FIPS       Date     Counts 
1001       Jan_22    0
1003       Jan_22    1
1004       Jan_22    0
1001       Jan_23    1
1003       Jan_23    5
1004       Jan_23    0
1001       Jan_24    6
1003       Jan_24    10
1004       Jan_24    1 
1001       Jan_25    8
1003       Jan_25    12
1004       Jan_25    3

Result:
    FIPS   Date     Counts
    1001   Jan_23   1
    1003   Jan_22   1
    1004   Jan_24   1

I am trying to find the number of days since the 1st case of Covid-19 within different counties in the United States. I need to find the first occurrence of 1 case confirmed in that county and then get R to tell me for each FIPS code is on which date was it that there the first day it was confirmed for 1 case. I am doing all 3141 counties. The dates go from Jan_22 to April_30.

Edit3: And the number of observations is 314200, with 3 variables.

t.first <- data_long[match(unique(data_long$Counts, data_long$Counts))]`

I want to use t.first to try to get the first occurrence in that data.


Solution

  • You can keep rows where Counts > 0 and then for each FIPS select the 1st row.

    library(dplyr)
    df %>%
      filter(Counts > 0) %>%
      group_by(FIPS) %>%
      slice(1L)
    
    #   FIPS Date   Counts
    #  <int> <chr>   <int>
    #1  1001 Jan_23      1
    #2  1003 Jan_22      1
    #3  1004 Jan_24      1