I used the code below to generate a dataframe in r:
data_5 <- data.frame(id=factor(rep(c("A","B","c"),each=214)),
You could use the
ave function as below (hope I understood your question correctly). Using the syntax of the first line, the world is pretty much your oyster and you can specify any function you would like to be applied to the different id categories. You may also want to check out the
tapply functions to apply functions to different categories.
data_5$perc<- ave(data_5$people, data_5$id, FUN = function(x) x/sum(x)) data_5$mean <- ave(data_5$people, data_5$id, FUN = mean) data_5$cummean <- ave(data_5$people, data_5$id, FUN = cummean)