[英]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 .我想严格遵守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
函数:
Reduce(`+`, list(test1, test1, test1)) / 3
Is there more simpler way to do it without going back and forth using list
object?有没有更简单的方法可以做到这一点,而无需使用list
对象来回切换?
We may loop over the array
in the 1st two dimensions and get the mean
我们可以遍历第一个二维的array
并得到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
或遍历单个维度并获取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
这两种方法都比使用+
的Reduce
方法更好,尤其是当在mean
和rowMeans/colMeans
中都发现na.rm
参数时存在缺失值时
NOTE: replicate
is used to create an array
by replicating the object 'test1' n
times.注意: replicate
用于通过复制对象“test1” n
次来创建array
。
If the object is already a list
of table
s, then convert to array
with simplify2array
before applying the apply
如果对象已经是table
的list
,则在应用apply
之前使用simplify2array
转换为array
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.