我有一个 58 列的数据框,我需要将转换 $log(x_{i,j}+1)$ 应用于前 56 列中的所有值。我可以使用什么方法来最有效地解决这个问题?我假设有一些东西可以让我做到这一点,而不仅仅是使用一些 for 循环来运行整个数据帧。
alexwhan 的答案对于 log 来说是正确的(并且可能应该被选为正确答案)。然而,它的工作原理非常清晰,因为日志是矢量化的。我经常经历非向量化函数的特殊痛苦。当我开始使用 R 时,并且不太了解 apply 系列,我经常使用丑陋的循环。因此,对于那些可能偶然发现这个问题但没有向量化函数的人来说,我提供了以下概念证明。
#Creating sample data
df <- as.data.frame(matrix(runif(56 * 56), 56, 56))
#Writing an ugly non-vectorized function
logplusone <- function(x) {log(x[1] + 1)}
#example code that achieves the desired result, despite the lack of a vectorized function
df[, 1:56] <- as.data.frame(lapply(df[, 1:56], FUN = function(x) {sapply(x, FUN = logplusone)}))
#Proof that the results are the same using both methods...
#Note: I used all.equal rather than all so that the values are tested using machine tolerance for mathematical equivalence. This is probably a non-issue for the current example, but might be relevant with some other testing functions.
#should evaluate to true
all.equal(log(df[, 1:56] + 1),as.data.frame(lapply(df[, 1:56], FUN = function(x) {sapply(x, FUN = logplusone)})))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)