1

I have the following data frame:

1  8.03    0.37     0.55    1.03     1.58    2.03 15.08   2.69    1.63    3.84   1.26 1.9692516

2  4.76    0.70       NA    0.12     1.62    3.30  3.24   2.92    0.35    0.49   0.42        NA

3  6.18    3.47     3.00    0.02     0.19   16.70  2.32  69.78    3.72    5.51   1.62 2.4812459

4  1.06   45.22     0.81    1.07     8.30  196.23  0.62 118.51   13.79   22.80   9.77 8.4296220

5  0.15    0.10     0.07    1.52     1.02    0.50  0.91   1.75    0.02    0.20   0.48 0.3094169

7  0.27    0.68     0.09    0.15     0.26    1.54  0.01   0.21    0.04    0.28   0.31 0.1819510

I want to calculate the geometric mean for each row. My codes is

dat <- read.csv("MXreport.csv")
if(any(dat$X18S > 25)){  print("Fail!") } else { print("Pass!")}
datpass <- subset(dat, dat$X18S <= 25)
gene <- datpass[, 42:52]
gm_mean <- function(x){   prod(x)^(1/length(x))}
gene$score <- apply(gene, 1, gm_mean)
head(gene)

I got this output after typing this code:

1  8.03   0.37   0.55    1.03     1.58    2.03 15.08   2.69    1.63    3.84   1.26 1.9692516

2  4.76    0.70       NA    0.12     1.62    3.30  3.24   2.92    0.35    0.49   0.42        NA

3  6.18    3.47     3.00    0.02     0.19   16.70  2.32  69.78    3.72    5.51   1.62 2.4812459

4  1.06   45.22     0.81    1.07     8.30  196.23  0.62 118.51   13.79   22.80   9.77 8.4296220

5  0.15    0.10     0.07    1.52     1.02    0.50  0.91   1.75    0.02    0.20   0.48 0.3094169

7  0.27    0.68     0.09    0.15     0.26    1.54  0.01   0.21    0.04    0.28   0.31 0.1819510

The problem is I got NA after applying the geometric mean function to the row that has NA. How do I skip NA and calculate the geometric mean for the row that has NA

When I used gene<- na.exclude(datpass[, 42:52]). It skipped the row that has NA and not calculate the geometric mean at all. That is now what I want. I want to also calculate the geometric mean for the row that has NA also. How do I do this?

smci
  • 32,567
  • 20
  • 113
  • 146
Tim Tran
  • 11
  • 3
  • Try to include a condition within your function that excludes NAs. In that case you'll exclude the NAs of each row and not all the rows with NAs. – AntoniosK Aug 22 '15 at 08:43
  • 2
    Perhaps `gm_mean <- function(x){ prod(x[!is.na(x)])^(1/length(x[!is.na(x)]))}` – akrun Aug 22 '15 at 08:43
  • 2
    Have a look at this answer which has a geometric mean function tolerant of NA: http://stackoverflow.com/a/25555105/2773500. – MikeRSpencer Aug 22 '15 at 11:23
  • This `exp(mean(log(x), na.rm=T))` from [R: Geometric mean](http://www.r-bloggers.com/r-geometric-mean/). It is also the same function at the center of the above linked SO page. – Pierre L Aug 22 '15 at 12:03
  • 1
    Possible duplicate of [Geometric Mean: is there a built-in?](https://stackoverflow.com/questions/2602583/geometric-mean-is-there-a-built-in) – smci Jan 12 '19 at 16:01
  • @MikeRSpencer: yes this question is a duplicate. – smci Jan 12 '19 at 16:02

0 Answers0