The difference between the average and manual calculations in R?

I am writing a simple function in R to calculate the percentage differences between two input numbers.

pdiff <-function(a,b) 
    {
      if(length(a>=1)) a <- median(a)
      if(length(b>=1)) b <- median(b)
      (abs(a-b)/((a+b)/2))*100
    }

    pdiffa <-function(a,b)
    {
      if(length(a>=1)) a <- median(a)
      if(length(b>=1)) b <- median(b)
      (abs(a-b)/mean(a,b))*100
    }

When you run it with a random value a and b, the functions give different results

x <- 5
y <- 10
pdiff(x,y) #gives 66%
pdiffa(x,y) #gives 100%

enter image description here

When I enter the code, apparently, the values ​​(x + y) / 2 = 7.5 and the average (x, y) = 5 are different ...... I missed something really obvious and stupid here?

enter image description here

+4
source share
2 answers

This is due to the nasty "gotcha" in the function mean()(not listed on the R trap list , but probably should be): you want mean(c(a,b)), not mean(a,b). From ?mean:

(x,...)
[snip snip snip]
... , .

, , mean(5,10)? mean mean.default, trim :

trim ( 0 0,5) , x . .

" " , trim 0,5 0,5, , mean 50% , , . mean.default, , ...

if (trim >= 0.5) 
      return(stats::median(x, na.rm = FALSE))

, mean(c(x,<value_greater_than_0.5>)) c(5), 5...

+12

mean(5, 10) .

mean(5, 10)
[1] 5

mean(c(5, 10)).

mean(c(5, 10))
[1] 7.5

mean .

+4

Source: https://habr.com/ru/post/1673446/


All Articles