在数据框列表中,计算满足条件的比例

In a list of data frames, calculate proportion of these that meets a condition

我有一个数据框列表 (gtf),我想计算在一组坐标 [i,j](坐标 [i, j] == 0).即,如果我有 500 个数据帧,其中 120 个数据帧在坐标 [1,1] 处为 0,则函数将为 return 120/500。

最终,我希望我的函数 return 一个数据框,其中的列对应于 gtf 的列,并且值是在相应坐标中具有 0 的数据框的比例.这是 gtf 的重现:

x = matrix(c(1,0), 6,6)
x = as.data.frame(x)
y = matrix(c(0,1), 6,6)
y = as.data.frame(x)
gtf = list(x,y,x,y)

这是我尝试过的:

for (i in seq_along(gtf)) 
    for (j in seq_along(gtf[[i]])) 
        if (gtf[[i]][1,j] == 0) tf[[i]] <- TRUE

我们使用 ==list 元素转换为逻辑矩阵,Reduce 通过添加相应的 'i,j' 元素并将其除以 [=] 将其转换为单个数据集list

的 14=]
Reduce(`+`, lapply(gtf, `==`, 0))/length(gtf)