Search code examples
rdatatable

How to compare the value in the same day in r?


I want to compare the signal within the same day, and try to use for loop to handle this problem, but I think there should have more efficient function to solve this problem.

The given dataset:

Signal Date
1 2000-01-01
2 2000-01-01
2 2000-01-01
2 2000-01-02
5 2000-01-02

My expected output:

Signal Date
2 2000-01-01
5 2000-01-02

Solution

  • I think we can infer from your comments that Signal is actually a character or factor vector which should be converted to numeric before attempting to order it - otherwise a signal of "2" will be higher than a signal of "10" due to alphabetic ordering.

    In any case, the example data you posted can be coerced on the fly if required, so the following code should work whatever the format is (numeric, character, or factor):

    library(tidyverse)
    
    df %>%
      group_by(Date) %>%
      slice_max(as.numeric(as.character(Signal)), with_ties = FALSE)
    #> # A tibble: 2 x 2
    #> # Groups:   Date [2]
    #>   Signal Date      
    #>   <chr>  <chr>     
    #> 1 2      2000-01-01
    #> 2 5      2000-01-02
    

    Or better still

    df %>%
      mutate(Signal = as.numeric(as.character(Signal))) %>%
      group_by(Date) %>%
      slice_max(Signal, with_ties = FALSE)
    #> # A tibble: 2 x 2
    #> # Groups:   Date [2]
    #>   Signal Date      
    #>   <dbl>  <chr>     
    #> 1 2      2000-01-01
    #> 2 5      2000-01-02
    

    Data from question in reproducible format

    df <- structure(list(Signal = c("1", "2", "2", "2", "5"), Date = c("2000-01-01", 
    "2000-01-01", "2000-01-01", "2000-01-02", "2000-01-02")), row.names = c(NA, 
    -5L), class = "data.frame")