I have a dataframe "Upton of the kinetics of the anti-asthmatic drug theophylline" with tasks.link to dataset
A piece of dataframe is in the picture. One of the tasks is "Only average concentration per subject should be reported". I wanted to calculate average concentration for each subject (there are 12 factors in total) and then replace the value of conc to these average value.
I tried to create a function:
conc_mean <- function(x, df){
mean_x = apply(subset(df, Subject==x, select = conc), 2, FUN=mean)
apply(df$Subject, MARGIN = 1,
FUN = function(x)
{
if(Subject == x)
conc=mean_x
}
)
}
Mean is counted right, but apply() doesn`t work :
apply(df$Subject, MARGIN = 1, FUN = function(x) { :
dim(X) must have a positive length
How can these problem be solved?