如何按因子相关统计与R中的DPLYR分组

时间:2018-01-22 22:31:34

标签: r dplyr

我试图提取cor.test(我的意思是:$ estimate和$ p.value)的结果,按因子分组。使用的数据框是这样的:

  'data.frame': 8940 obs. of  3 variables:
 $ Code   : Factor w/ 864 levels "420639","427354",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ TxP    : num  0.0107 0.0107 0.0107 0.0239 0.0239 ...
$ Vol    : num  63193 59854 43229 38486 37028 ...

使用dplyr包,我写的代码是:

‘CorResult <- function(data, x, y, Grped){
  x <- x
  y <- y
  fac <- as.name(as.character(Grped)[2])
  corR <- interp(~ cor.test(x, y,method="pearson",na.action = "na.exclude")$estimate, x = x, y = y)
  Pval <- interp(~ cor.test(x, y,method="pearson",na.action = "na.exclude")$p.value, x = x, y = y)
  summarise_( group_by_(data, fac), R=corR, Pval=Pval)
  }
CorResult  (data = data4, data4$Vol, data4$TxP, Grped= ~Code )

我收到的结果总是相同的值,无论Groupe by!

'# A tibble: 864 x 3
   Code        R                             pval
   <fct>   <dbl>                            <dbl>
 1 420639  0.117 0.000000000000000000000000000134
 2 427354  0.117 0.000000000000000000000000000134
 3 429885  0.117 0.000000000000000000000000000134
 4 1625921 0.117 0.000000000000000000000000000134
 5 1625922 0.117 0.000000000000000000000000000134
 6 1631209 0.117 0.000000000000000000000000000134
# ... with 854 more rows

我哪里做错了?我错过了什么。任何一只手都会受到赞赏。

0 个答案:

没有答案