Home > Net >  How to list min and max values for multiple values of a given name in a data frame in R?
How to list min and max values for multiple values of a given name in a data frame in R?

Time:09-21

I have a dataframe that includes a column 'names' with multiple instances of a given name, against which another column, 'time' has various times. I'm trying to convert this raw data into another dataframe that lists only unique values for the names, and gives minimum and maximum values for times in separate columns, as well as a count of instances of each name from the original dataframe. I want the final dataframe to be sorted by min.time, smallest to largest.

For example:

data example

So far I have got the unique list of names and the count of each to work, but I can't figure out how to find the minimum and maximum time value for each unique name. Any suggestions would be much appreciated.

CodePudding user response:

Using tidyverse:

library(tidyverse)

df <- 
tribble(
  ~name, ~time,
  'a', 19.642,
  'a', 19.644,
  'b', 20.178,
  'c', 22.345,
  'b', 20.183,
  'a', 19.646,
  'b', 20.190,
  'c', 22.332)

df %>% 
  group_by(name) %>% 
  summarise(count = n(), min.time = min(time), max.time = max(time)) %>% 
  arrange(min.time)
#> # A tibble: 3 × 4
#>   name  count min.time max.time
#>   <chr> <int>    <dbl>    <dbl>
#> 1 a         3     19.6     19.6
#> 2 b         3     20.2     20.2
#> 3 c         2     22.3     22.3

Created on 2021-09-20 by the reprex package (v2.0.0)

CodePudding user response:

use dplyr package dataset %>% group_by(name) %>% summarize(n= n(), min = min(time), max = max(time))

CodePudding user response:

Edit

Based on your comment below, this is a potential solution:

library(tidyverse)
df <- data.frame(name = c("a", "a", "b", "c", "b", "a", "b", "c"),
                 time = c(19.642, 19.644, 20.178, 22.345,
                          20.183, 19.646, 20.190, 22.332))
df2 <- df %>%
  group_by(name) %>%
  summarise(n = n(),
            min = min(time),
            max = max(time))
print.data.frame(df2)
#>   name n    min    max
#> 1    a 3 19.642 19.646
#> 2    b 3 20.178 20.190
#> 3    c 2 22.332 22.345

df3 <- df2 %>%
  arrange(desc(min)) %>%
  mutate(`max - min` = max - min)
print.data.frame(df3)
#>   name n    min    max max - min
#> 1    c 2 22.332 22.345     0.013
#> 2    b 3 20.178 20.190     0.012
#> 3    a 3 19.642 19.646     0.004

Created on 2021-09-21 by the reprex package (v2.0.1)

  • Related