-1

I am using this code to calculate the means of different groups of values.

> means <- aggregate(jots.xpc.txt$Cfxtn32, by=list(jots.xpc.txt$Tx), mean)
> means
   Group.1          x
1   C1_Mac 0.04957707
2    C1_MH 0.14721646
3   C2_Mac 0.01389217
4    C2_MH 0.03080142
5   X1_Mac 0.04871321
6    X1_MH 0.15913916
7   X2_Mac 0.07951835
8    X2_MH 0.08354484
9  XB1_Mac 0.05090939
10  XB1_MH 0.22035204
11 XB2_Mac 0.05053910
12  XB2_MH         NA

But I want to exclude the NA from the data, so I used the na.rm=TRUE command.

> means <- aggregate(jots.xpc.txt$Cfxtn32, by=list(jots.xpc.txt$Tx), mean(na.rm=TRUE))

Error in mean.default(na.rm = TRUE) :
argument "x" is missing, with no default

Can someone tell me what I'm doing wrong?

user20650
  • 24,654
  • 5
  • 56
  • 91
AnisaW
  • 7
  • 2
  • 2
    Try `... mean, na.rm=TRUE)` instead (notice there is no opening bracket after `mean` (ps the formula method imo is a wee bit neater in this case `aggregate(fxtn32 ~ Tx, data=jots.xpc.txt, mean, na.rm=TRUE)`) – user20650 Oct 16 '17 at 19:25

2 Answers2

2

na.rm=T must be passed to aggregate's ... argument so that aggregate can pass it on to mean.

> data1 <- data.frame(x = c(1,3,4,5,NA,7,NA,6), y = c('A','A','B','B','A','A','B','B'))
> 
> aggregate(data1$x, FUN = mean, by = list(data1$y))
  Group.1  x
1       A NA
2       B NA
> 
> 
> aggregate(data1$x, FUN = mean, by = list(data1$y), na.rm = T)
  Group.1        x
1       A 3.666667
2       B 5.000000

Thanks gregor for the paraphrasing.

amrrs
  • 6,215
  • 2
  • 18
  • 27
  • 2
    A suggested edit to your phrasing: it must be passed to `aggregate`'s `...` argument so that `aggregate` can pass it on to `mean`. Too many people assume functions like `lapply` and `aggregate` have an `na.rm` option, when really they can just pass arguments on through to whatever function is used. – Gregor Thomas Oct 16 '17 at 19:31
0

Also exactly the type of problem that dplyr is designed to handle well. So you could also do it like this:

library(dplyr)

means <- jots.xpc.txt %>%
   group_by(Tx) %>%
   summarise(Cfxtn32_mean = mean(Cfxtn32, na.rm = TRUE))
boshek
  • 4,100
  • 1
  • 31
  • 55