-2

From data like this:

data.frame(aid = c(1,2,1,2), time = c(3,1,4,1))

How is it possible to receive the median for time for all aid option 1,2

Example output:

data.frame(aid = c(1,2), time_median = c(3.5, 1))
jay.sf
  • 33,483
  • 5
  • 39
  • 75
foc
  • 907
  • 1
  • 9
  • 25

1 Answers1

0

This is pretty easy using the package dplyr.

library(dplyr)
#> 
#> Attache Paket: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
df <- data.frame(aid = c(1,2,1,2), time = c(3,1,4,1))

df %>% group_by(aid) %>% 
  summarize(median = median(time))
#> `summarise()` ungrouping output (override with `.groups` argument)
#> # A tibble: 2 x 2
#>     aid median
#>   <dbl>  <dbl>
#> 1     1    3.5
#> 2     2    1

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

Tim-TU
  • 348
  • 1
  • 3
  • 12