1

I am not able to generate the mean of the value column. This is a similar/duplicate question that I posted earlier.

library(dplyr)

df <- data.frame(Dose   = c(1, 1, 1, 1, 1, 1, 10, 10, 10, 10, 10, 10),
                 Route  = c('IV','IV','IV','PO','PO','PO','IV','IV','IV','PO','PO','PO'),
                 Timepoint = c(0.25,0.25,0.25,0.25,0.25,0.25,0.5,0.5,0.5,0.5,0.5,0.5),
                 value =    c(207,181,201,505,180,309,123,122,137,441,335,402))

mean.df <- df %>% aggregate(value~Timepoint + Dose + Route,  FUN = mean)

Error in aggregate.data.frame(., value ~ Timepoint + Dose + Route, FUN = mean) : 
'by' must be a list

When I try this:

  mean.df <-  df %>% group_by(Timepoint, Dose, Route) %>% summarize(mean_value=mean(value))

I get this instead of a table with mean values based on Timepoint, Dose and Route.

  mean_value
  1   261.9167

What am I missing?

RanonKahn
  • 853
  • 10
  • 34

1 Answers1

2

We can specify the data argument as .

library(dplyr)
df %>% 
     aggregate(value~Timepoint + Dose + Route, data = ., FUN = mean)

summarize can be from dplyr or plyr. If both packages are loaded, there is a chance that plyr::summarize masked the dplyr::summarize. So, we can specify the package with ::

df %>% 
  group_by(Timepoint, Dose, Route) %>% 
  dplyr::summarize(mean_value=mean(value), .groups = 'drop')

-output

# A tibble: 4 x 4
#  Timepoint  Dose Route mean_value
#*     <dbl> <dbl> <chr>      <dbl>
#1      0.25     1 IV          196.
#2      0.25     1 PO          331.
#3      0.5     10 IV          127.
#4      0.5     10 PO          393.
akrun
  • 874,273
  • 37
  • 540
  • 662