I have the following data set:
df <- data.frame(
C = c(1,2,3,1,2,3,1,2,3,1),
weight = c(1,1.5,2,2,1.5,1,2,1,1.5,2.5),
time = c(15,20,30,45,60,15,20,30,45,60)
)
I need to aggregate the data by the variable C in order to find the median time for each C. Each observation is weighted by the variable 'weight'.
Is there a way to replace 'mean' by a weighted median in the following code ?
output<-aggregate(.~C, data=df, mean, na.rm=TRUE)