how to combine data in a column with the same "name" and select only the highest "distance covered" by the particular individual ? ?

name = arun, arup, shiva, prince, arun, shiva, arup, prince, arun, arup, prince, shiva
distance covered= 20, 50, 40, 38, 65, 22, 55, 85, 92, 44, 62, 88

To do this, I'd look at {dplyr}, specifically grouping. The Data Transformation chapter of R for Data Science may be worth a read:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
set.seed(123)

# invent some data
name <- sample(c("A", "B", "C"), size = 10, replace = TRUE)
values <- sample(1:100, size = 10)
dat <- tibble(name, values)

# get highest per name
dat %>%
  group_by(name) %>%
  filter(values == max(values))
#> # A tibble: 3 × 2
#> # Groups:   name [3]
#>   name  values
#>   <chr>  <int>
#> 1 C         90
#> 2 B         97
#> 3 A         72

# or, using newer {dplyr} syntax
filter(dat, values == max(values), .by = name)
#> # A tibble: 3 × 2
#>   name  values
#>   <chr>  <int>
#> 1 C         90
#> 2 B         97
#> 3 A         72

Created on 2023-05-06 with reprex v2.0.2

1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.