Search code examples
rdataframesummarize

min and max for specific rows in a column


           X             Y
 1:        2          1.973203
 2:        2          1.985226
 3:        2          1.988916
 4:        2          1.998701
 5:        2          1.998701
 6:        5          4.726402
 7:        5          4.854796
 8:        5          4.891613
 9:        5          4.975043
10:        5          4.925852
11:       10          8.808622
12:       10          9.311003
13:       10          9.383758
14:       10          9.719726
15:       10          9.496574
16:       30         19.758713
17:       30         22.318520
18:       30         23.377183
19:       30         24.593082
20:       30         22.787362

Hi, I wanted to find the min and max of each x. For example, min and max of 2 (i.e. X) and min & max of 5 , 10 and 30. For each X, there are 5 values of Y. So, I wanted to find min and max and each of X. I was doing it in a lengthy way. Is there a simpler way to do it? Thanks!


Solution

  • To have min and max value in different column you can do :

    library(dplyr)
    
    df %>% group_by(X) %>%summarise(min_Y = min(Y), max_Y = max(Y)) %>% ungroup
    
    #      X min_Y max_Y
    #  <int> <dbl> <dbl>
    #1     2  1.97  2.00
    #2     5  4.73  4.98
    #3    10  8.81  9.72
    #4    30 19.8  24.6 
    

    If you want the values to be in same column :

    df %>% group_by(X) %>% summarise(Y = range(Y), val = c('min', 'max')) %>% ungroup
    
    #      X     Y val  
    #  <int> <dbl> <chr>
    #1     2  1.97 min  
    #2     2  2.00 max  
    #3     5  4.73 min  
    #4     5  4.98 max  
    #5    10  8.81 min  
    #6    10  9.72 max  
    #7    30 19.8  min  
    #8    30 24.6  max