Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding the average of R table object

Other questions using "table" in their title are actually using data frame.

I want to keep this strictly about table object.

Suppose I have tables with same structure that I want to find the average of.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

For example:

test1 <- head(table(iris$Sepal.Length, iris$Species))

(test1 + test1 + test1) / 3

> (test1 + test1 + test1) / 3
     
      setosa versicolor virginica
  4.3      1          0         0
  4.4      3          0         0
  4.5      1          0         0
  4.6      4          0         0
  4.7      2          0         0
  4.8      5          0         0

However, it cannot be done by:

> mean(c(test1,test1,test1))
[1] 0.8888889
> sum(c(test1,test1,test1)) / 3
[1] 16

Best approach I could find was to make the objects into a list of tables and use Reduce function:

Reduce(`+`, list(test1, test1, test1)) / 3

Is there more simpler way to do it without going back and forth using list object?

>Solution :

We may loop over the array in the 1st two dimensions and get the mean

apply(replicate(3, test1), 1:2, mean, na.rm = TRUE)

-output

       setosa versicolor virginica
  4.3      1          0         0
  4.4      3          0         0
  4.5      1          0         0
  4.6      4          0         0
  4.7      2          0         0
  4.8      5          0         0

Or loop over a single dimension and get the rowMeans/colMeans

apply(replicate(3, test1), 2, rowMeans, na.rm = TRUE)
     
      setosa versicolor virginica
  4.3      1          0         0
  4.4      3          0         0
  4.5      1          0         0
  4.6      4          0         0
  4.7      2          0         0
  4.8      5          0         0

Both these methods are better than the Reduce approach with + especially when there are missing values as na.rm argument is found in both mean and rowMeans/colMeans

NOTE: replicate is used to create an array by replicating the object ‘test1’ n times.


If the object is already a list of tables, then convert to array with simplify2array before applying the apply

apply(simplify2array(list(test1, test1, test1)), 1:2, mean, na.rm = TRUE)
     
      setosa versicolor virginica
  4.3      1          0         0
  4.4      3          0         0
  4.5      1          0         0
  4.6      4          0         0
  4.7      2          0         0
  4.8      5          0         0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading