如何计算R中的相关性
我想计算 RI 中数据集 x 的子集的列之间的相关系数,其中有 40 行模型,每行 200 个模拟,总共 8000 行 我想计算每个模拟(40 行)的列之间的相关系数
cor(x[c(3,5)])
从所有 8000 行计算
我需要 cor(x[c(3,5)])
但只有当 X$nsimul=1
等时
你才会在这方面帮助我 桑
I wanted to calculate correlation coeficient between colunms of a subset of a data set x in R
I have rows of 40 models each 200 simulations in total 8000 rows
I wanted to calculate the corr coeficient between colums for each simulation (40 rows)
cor(x[c(3,5)])
calculates from all 8000 rows
I need cor(x[c(3,5)])
but only when X$nsimul=1
and so on
would you help me in this regards
San
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您到底在使用
x[c(3,5)]
做什么,但看起来您想要执行如下操作:您有一个数据框X
像这样:并且您想按
nsimul
列分割此数据帧,并计算a
和b
之间的相关性> 每组。这是一个经典的split-apply-combine
问题,plyr
包非常适合:I'm not sure what exactly you're doing with
x[c(3,5)]
but it looks like you want to do something like the following: You have a data-frameX
like this:And you want to split this data-frame by the
nsimul
column, and calculate the correlation betweena
andb
in each group. This is a classicsplit-apply-combine
problem for which theplyr
package is very well-suited:您可以使用
by
函数,例如:You can use
by
function e.g.: