I want to calculate the median age for each group in this data frame:
dfx<-data.frame(group=c(1:100),
`1`=rnorm(100,50,0.5),
`2`=rnorm(100,45,15),
`3`=rnorm(100,17,5))
colnames(dfx) <- c("group", "1","2","3")
Which shows the number of individuals aged 1 - 3 for different groups (the real dataframe has over 100 columns and runs from age 0 - 90+).
# A tibble: 100 × 4
group `1` `2` `3`
<int> <dbl> <dbl> <dbl>
1 1 49.7 39.5 15.2
2 2 49.0 41.4 20.3
3 3 49.5 74.8 8.31
4 4 50.0 34.4 18.2
5 5 49.9 41.5 17.1
6 6 49.7 40.4 21.0
7 7 49.6 72.6 23.8
8 8 50.4 41.9 14.9
9 9 50.3 63.8 17.8
10 10 50.0 34.7 26.2
# … with 90 more rows
I want to calculate the median age for each group to produce something like this:
dfxx<-data.frame(group=c(1:100),
med_age=rnorm(100,2,0.0001))
Note that these are meant to be integers, just like the original age data.
# A tibble: 100 × 2
group med_age
<int> <dbl>
1 1 2.00
2 2 2.00
3 3 2.00
4 4 2.00
5 5 2.00
6 6 2.00
7 7 2.00
8 8 2.00
9 9 2.00
10 10 2.00
# … with 90 more rows
I assume I need to make a vector/histogram from the data before calculating the median, but I'm not sure how to do this.
Thanks in advance!