按组进行数据测试的数据表 [英] Data.table with cor.test by group

查看:74
本文介绍了按组进行数据测试的数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 data.table 通过cor.test函数计算每个组的相关性时,它对于默认方法(皮尔逊)可以正常工作,但是不是为了长矛手。我收到 data.table 错误。

When I use data.table to compute correlations for each group with the cor.test function, it works fine for the default method (which is "pearson") but not for "spearman". I receive a data.table error.

library("data.table")
dd <- data.table(group=sample(letters[1:3], 50, replace=TRUE), x=rnorm(50), y=rnorm(50))
head(dd)
## group          x            y
## 1:     c  0.1808595  2.124721051
## 2:     a  0.2492086  0.112128546
## 3:     b -1.6392331 -1.823208890
## 4:     c  0.6605648  0.981215691
## 5:     c -0.4625216 -0.008350339
## 6:     b -0.2747395  1.045594928
dd[ , cor.test(x, y), by=group]  # works
dd[ , cor.test(x, y, method="spearman"), by=group]  # does not work
## Error in `[.data.table`(dd, , cor.test(x, y, method = "spearman"), by = group) : 
## Column 2 of j's result for the first group is NULL. [...]

有人对使用cor.test分组使用有任何想法吗? data.table 不会导致错误?或者,如果这根本不是 data.table 可以修复的任何东西,因为它与此处的cor.test胆量有关,那么其他任何可比较的(data.frame ,dplyr)按有效小组使用spearman cor.test的方式?

Does anyone have any idea of a way to use cor.test by group with data.table that does not result in error? Or, if this is not anything fix-able by data.table at all because it has to do with the guts of cor.test here, any other comparable (data.frame, dplyr) way of using spearman cor.test by group that works?

推荐答案

问题是因为 parameter 元素在列表结果中返回 cor.test method = spearman 的值为 NULL ,这会导致data.table崩溃。

The problem is because the parameter element returned in the list result of cor.test for method="spearman" is NULL, which causes data.table to freak out.

返回的错误消息清楚地表明了这一点:

The error message returned states this pretty explicitly:


j的第2列第一组的结果为NULL。 [...]

Column 2 of j's result for the first group is NULL. [...]

然后从结果中删除第2列即可进行设置。

Just drop column 2 from the result then, and you'll be set.

dd[ , cor.test(x, y,method="spearman")[-2], by=group]

#   group statistic   p.value   estimate null.value alternative   ...    
#1:     c      2060 0.6263233  0.1043478          0   two.sided   ...
#2:     a       262 0.5762578 -0.1909091          0   two.sided   ...
#3:     b       650 0.5667271 -0.1607143          0   two.sided   ...

这篇关于按组进行数据测试的数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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