我无法在线找到此问题的解决方案,就像看起来那么简单。 这里,它是: #Construct test dataframe
tf <- data.frame(1:3,4:6,c("A","A","A"))
#Try the apply function I'm trying to use
test <- apply(tf,2,function(x) if(is.numeric(x)) mean
我经常需要对数据框/矩阵中的每对列应用函数,并将结果以矩阵形式返回。现在我总是写一个循环来做到这一点。例如,为了使含我写相关的p值的矩阵: df <- data.frame(x=rnorm(100),y=rnorm(100),z=rnorm(100))
n <- ncol(df)
foo <- matrix(0,n,n)
for (i in 1:n)
{
for (j in