I have formated my data in long
df1<-read.table(text=" ID Temp location
1 12 4
1 18 3
1 17 5
1 10 1
1 19 1
1 15 4
1 16 5
1 10 3
1 11 5
1 15 1
2 20 3
2 10 3
2 17 1
2 13 5
2 12 1
2 14 4
2 20 5
2 13 1
2 13 3
2 10 3
3 12 4
3 18 3
3 18 3
3 15 1
3 17 1
3 15 4
3 10 1
3 11 3
3 13 1
3 14 1",header=TRUE)
I want to calculate the median ( round up) based on Temp and location for 3 groups (Id). The question is what is the median for id1, id2,id3, if location=1. In other words, 10,19 and 15, give a median of 15 or for id2, we have 17,12 and 13, give a median of 13.5, roundup=14. and so on.
So I need to get this data:
AM1 15
AM2 14
AM3 14
Thanks for your help and sorry I was unable to show my effort.
You could also use data.table.
library(data.table)
setDT(df1)[location == 1, .(Median = base::round(median(as.numeric(Temp)))), by = .(ID = paste0(“AM”, ID))]