如何使用ddply与多个列建立关联? [英] How can correlate against multiple columns using ddply?

查看:89
本文介绍了如何使用ddply与多个列建立关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a data.frame and I want to calculate correlation coefficients using one column against the other columns (there are some non-numeric columns in the frame as well).

ddply(Banks,.(brand_id,standard.quarter),function(x) { cor(BLY11,x) })
# Error in cor(BLY11, x) : 'y' must be numeric

我针对is.numeric(x)进行了测试

I tested against is.numeric(x)

ddply(Banks,.(brand_id,standard.quarter),function(x) { if is.numeric(x) cor(BLY11,x) else 0 })

,但是每次比较都失败,返回0,仅返回一列,就好像它只被调用过一次一样.传递给该函数的是什么?刚来到R时,我认为我缺少一些基本知识.

but that failed every comparison and returned 0 and returned only one column, as if its only being called once. What is being passed to the function? Just coming to R and I think there's something fundamental I'm missing.

谢谢

推荐答案

尝试类似的方法

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

这篇关于如何使用ddply与多个列建立关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆