Search code examples
raverage

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.

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