0

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.  A piece of dataframe

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?

zephryl
  • 14,633
  • 3
  • 11
  • 30
Yana Sal
  • 11
  • 4
  • 1
    Check the dplyr package and its function „group_by“. – deschen Oct 28 '22 at 22:26
  • 1
    `library(dplyr); df %>% group_by(Subject) %>% mutate(conc = mean(conc)) %>% ungroup()` – Jon Spring Oct 28 '22 at 22:28
  • Overall, if your just starting statistics/data science, I'd second that you should get well-acquainted with the dplyr package. You can read R for Data Science by Wickham; there is a free pdf. – James Oct 28 '22 at 22:41

1 Answers1

2

You can use R base ave function:

df <- read.csv("Pharmacokinetics_of_Theophylline_441_49.csv")
df$conc <- with(df, ave(conc, Subject))
head(df)
  X.1 X Subject   Wt Dose Time     conc
1   1 1       1 79.6 4.02 0.00 6.439091
2   2 2       1 79.6 4.02 0.25 6.439091
3   3 3       1 79.6 4.02 0.57 6.439091
4   4 4       1 79.6 4.02 1.12 6.439091
5   5 5       1 79.6 4.02 2.02 6.439091
6   6 6       1 79.6 4.02 3.82 6.439091
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138