本文介绍了在 r 中应用函数太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须为很多物种计算每行一个特定的公式.该公式是丰度值与数据框最后一行中存在的值之间的乘积.然后,将所有这些乘积相加.

I have to calculate for a lot of species a specific formula per row. The formula is a product between a value of abundance and a value present in the last row of the data frame. Then, all these products are summed.

我当前的脚本包括使用一个 apply 函数,它看起来和我开始使用的 for 循环一样慢.我在以下脚本中简化了问题,使用了一个名为 az 的简单 df:

My current script consists in using an apply function which appears to be as slow as the for-loop I started with.I simplified the problem in the following script, using a simple df called az :

az=data.frame(c(1,2,10),c(2,4,20),c(3,6,30))
colnames(az)=c("a","b","c")


# Initial for loop
prov=0 # prov for provisional number
    for (i in 1:nrow(az)){
            for (j in 1:ncol(az)){
                   prov=prov+az[i,j]*az[nrow(az),j]
            }
        print(prov)
        prov=0
        }

# Apply solution
apply(az[,], 1, function(x) {sum(x*az[nrow(az),], na.rm=TRUE)})

这两种解决方案都有效,但它们都很慢(使用我原来的 df),而且我必须对大量物种重复该操作.因此,我想知道是否有人有更有效的解决方案,也许使用向量化表达式.

Both solutions work but they are quite slow (with my original df) and I have to repeat the operation for a huge number of species.Thus, I was wondering if anyone has a more efficient solution, maybe using vectorized expressions.

亲切的问候.

推荐答案

最快的解决方案可能是矩阵代数:

The fastest solution is probably matrix algebra:

apply(az[,], 1, function(x) {sum(x*az[nrow(az),], na.rm=TRUE)})
#[1]  140  280 1400

m <- as.matrix(az)
m[is.na(m)] <- 0 #remove NA from sums
as.vector(m %*% m[nrow(m),])
#[1]  140  280 1400

这篇关于在 r 中应用函数太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:54