R 如何使用ddply关联多个列?

R 如何使用ddply关联多个列?,r,plyr,correlation,R,Plyr,Correlation,我有一个data.frame,我想用一列和其他列计算相关系数(框架中也有一些非数值列) 我测试了is.numeric(x) 但每次比较都失败了,返回0,只返回一列,就好像只调用了一次一样。传递给函数的是什么?刚来到R,我想我缺少了一些基本的东西 谢谢ddply将data.frame拆分为块并将它们(较小的data.frames)发送到您的函数。您的x是一个data.frame,其列与Banks相同。因此,是.numeric(x)是FALSEis.data.frame(x)应返回TRUE 尝试:

我有一个data.frame,我想用一列和其他列计算相关系数(框架中也有一些非数值列)

我测试了is.numeric(x)

但每次比较都失败了,返回0,只返回一列,就好像只调用了一次一样。传递给函数的是什么?刚来到R,我想我缺少了一些基本的东西


谢谢

ddply将data.frame拆分为块并将它们(较小的data.frames)发送到您的函数。您的
x
是一个data.frame,其列与
Banks
相同。因此,
是.numeric(x)
FALSE
is.data.frame(x)
应返回
TRUE

尝试:


看起来您正在做的事情也可以通过
sapply
完成:

with(Banks,
  sapply( list(brand_id,standard.quarter), function(x) cor(BLY11,x) )
)

此函数对块进行操作:

calc_cor_only_numeric = function(chunk) {
   is_numeric = sapply(chunk, is.numeric)
   return(cor(chunk[-is_numeric]))
 }
可由
ddply
使用:

ddply(banks, .(cat1, cat2), calc_cor_only_numeric)
我无法检查代码,但这应该可以让您开始了。

来自?cor:

如果“x”和“y”是矩阵,则协方差(或相关性) 计算“x”列和“y”列之间的距离

因此,您唯一真正的工作是删除非数字列:

# An example data.frame containing a non-numeric column
d <- cbind(fac=c("A","B"), mtcars)

## Calculate correlations between the mpg column and all numeric columns
cor(d$mpg, as.matrix(d[sapply(d, is.numeric)]))
     mpg       cyl       disp         hp      drat         wt     qsec
[1,]   1 -0.852162 -0.8475514 -0.7761684 0.6811719 -0.8676594 0.418684
            vs        am      gear       carb
[1,] 0.6640389 0.5998324 0.4802848 -0.5509251

试试这个

cor(longley[, 1], longley[ , sapply(longley, is.numeric)])



    GNP.deflator       GNP Unemployed Armed.Forces Population      Year  Employed
[1,]            1 0.9915892  0.6206334    0.4647442  0.9791634 0.9911492 0.9708985

这将适用于特定列,但如果我希望它与其他列相反,该怎么办?有100多个。您可以将它们写出来或使用类似于
lappy(columnnames,function(n)cor(x$BLY11,x[n])
的内容,其中
columnnames
是您要比较的列名向量。刚刚尝试了这个,收到了相同的错误:cor中的错误(BLY11,x):“y”必须是数字。您的列是数字吗?使用
is.numeric(银行$brand_id)
和类似工具进行检查。或者更一般地说,
sapply(名称(Banks),函数(x)类(Banks[,x,drop=TRUE])
与我的答案基本相同,带有
sed s/d/longley/yours
;)但我现在明白了,不需要显式地转换为矩阵。谢谢,太好了!如此接近,ddply(Banks,((brand_id,standard.quarter),function(x){cor(x$BLY11,x[,sappy(x,is.numeric)])}获得了相关性,但我丢失了列名。他们都是V1。。。V167。如何最好地获得原始列名?@LCricket将
cor(…)
包装在
as.data.frame()
中,以强制函数返回data.frame,而不是让
plyr
将其强制为一个。
ddply(banks, .(cat1, cat2), calc_cor_only_numeric)
# An example data.frame containing a non-numeric column
d <- cbind(fac=c("A","B"), mtcars)

## Calculate correlations between the mpg column and all numeric columns
cor(d$mpg, as.matrix(d[sapply(d, is.numeric)]))
     mpg       cyl       disp         hp      drat         wt     qsec
[1,]   1 -0.852162 -0.8475514 -0.7761684 0.6811719 -0.8676594 0.418684
            vs        am      gear       carb
[1,] 0.6640389 0.5998324 0.4802848 -0.5509251
cor(d$mpg, d[sapply(d, is.numeric)])

cor(mtcars, mtcars)
cor(longley[, 1], longley[ , sapply(longley, is.numeric)])



    GNP.deflator       GNP Unemployed Armed.Forces Population      Year  Employed
[1,]            1 0.9915892  0.6206334    0.4647442  0.9791634 0.9911492 0.9708985