Absolute Deviation Formula in R

224 views Asked by At

I have a little problem i wanna make this formula to my code in R studio for iris (data-mean of colum)/absolute deviation. and this is my code

y=iris[,1:4]
for(i in 1:4){y[, i] = (iris[,i] - mean(iris[,i]))/sd(iris[,i])}
y

but this code is for [(data -mean each column)/standar deviasi each colum] i try to reach how the formula of [(data -mean each column)/mean absolute deviation each column] but there is a problem with this code

y=iris[,1:4]
for(i in 1:4){y[, i] = (iris[,i] - mean(iris[,i]))/meanAD(iris)}
y

i think there is no problem but it took the mad cannot be calculated.

1

There are 1 answers

0
Robert On

You can do this:

(iris[,1:4] - apply(iris[,1:4],2,mean))/apply(iris[,1:4],2,mad)

   # > (iris[,1:4] - apply(iris[,1:4],2,mean))/apply(iris[,1:4],2,mad)
   #     Sepal.Length Sepal.Width  Petal.Length   Petal.Width
   # 1    -0.71624495 -0.13921489 -4.2814103923 -1.9198704978
   # 2     4.14287213  1.73504718 -3.7261867290 -0.9629158557
   # 3     0.50829624 -2.54700558 -1.3263186294 -5.4376802657
   # 4     3.27674035  0.09592757  0.2897098405 -6.4241497669