Deleting Single Dates Based on Another Value

Hi there, I am extremely new to RStudio and I am trying to figure out how to delete rows of data that only have a single date-detection per tag. For example, tag 242(code) only has one detection on 9/27/2009 (PT_Time_Date_Only). My advisor suggested I use loops, unique values, and/or NaN values but he is more familiar with other languages. Any help would be greatly appreciated!

Something like this. In R we try to avoid low level loops. Read the online book "R for Data Science" for an excellent introduction to modern R programming for beginners.

suppressMessages({
  library(lubridate)
  library(dplyr)
})
#> Warning: package 'lubridate' was built under R version 3.6.3

data <- data.frame(
  code = sample(208:211, 20, replace = TRUE),
  PT_Time_Date_Only = sample(seq(dmy("01012020"), dmy("05012020"), "days"), 20, replace = TRUE)
  ) %>%
  arrange(code, PT_Time_Date_Only)

data %>%
  group_by(code, PT_Time_Date_Only)  %>% # for each code and date
  mutate(n = n()) %>% # count records
  filter(n > 1) %>% # keep groups with with > 1 record
  ungroup() # tidy up
#> # A tibble: 14 x 3
#>     code PT_Time_Date_Only     n
#>    <int> <date>            <int>
#>  1   208 2020-01-02            2
#>  2   208 2020-01-02            2
#>  3   208 2020-01-04            3
#>  4   208 2020-01-04            3
#>  5   208 2020-01-04            3
#>  6   208 2020-01-05            2
#>  7   208 2020-01-05            2
#>  8   210 2020-01-03            2
#>  9   210 2020-01-03            2
#> 10   211 2020-01-03            2
#> 11   211 2020-01-03            2
#> 12   211 2020-01-05            3
#> 13   211 2020-01-05            3
#> 14   211 2020-01-05            3

Created on 2020-07-08 by the reprex package (v0.3.0)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.