对每个列组合应用函数 [英] Apply a function to each combination of columns
问题描述
我有一个包含 n
列的数据框,我想对每个 组合 列应用一个函数.这与 cor()
函数将数据帧作为输入并生成相关矩阵作为输出的方式非常相似,例如:
I have a data frame with n
columns and want to apply a function to each combination of columns. This is very similar to how the cor()
function takes a data frame as input and produces a correlation matrix as output, for example:
X <- data.frame(A=rnorm(100), B=rnorm(100), C=rnorm(100))
cor(X)
哪个将生成此输出:
> cor(X)
A B C
A 1.00000000 -0.01199511 0.02337429
B -0.01199511 1.00000000 0.07918920
C 0.02337429 0.07918920 1.00000000
但是,我有一个自定义函数,需要将其应用于每个列组合.我现在正在使用使用嵌套 for 循环的解决方案,该解决方案有效:
However, I have a custom function that I need to apply to each combination of columns. I am now using a solution that uses nested for loops, which works:
f <- function(x, y) sum((x+y)^2) # some placeholder function
out <- matrix(NA, ncol = ncol(X), nrow = ncol(X)) # pre-allocate
for(i in seq_along(X)) {
for(j in seq_along(X)) {
out[i, j] <- f(X[, i], X[, j]) # apply f() to each combination
}
}
产生:
> out
[,1] [,2] [,3]
[1,] 422.4447 207.0833 211.4198
[2,] 207.0833 409.1242 218.2430
[3,] 211.4198 218.2430 397.5321
<小时>
我目前正在尝试过渡到 tidyverse,并且希望避免使用 for 循环.有人可以向我展示针对这种情况的整洁解决方案吗?谢谢!
I am currently trying to transition into the tidyverse and would prefer to avoid using for loops. Could someone show me a tidy solution for this situation? Thanks!
推荐答案
You can do
library(tidyverse)
f <- function(x, y) sum((x+y)^2)
X <- data.frame(A=rnorm(100), B=rnorm(100), C=rnorm(100))
as.list(X) %>%
expand.grid(., .) %>%
mutate(out = map2_dbl(Var1, Var2, f)) %>%
as_tibble()
这篇关于对每个列组合应用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!