使用dplyr包获取p值以进行分组关联

问题描述 投票:-1回答:1

我正在尝试在数据帧中运行某些变量之间的相关性。我有一个字符向量(组),其余是数字。

数据帧< -

       Group    V1     V2    V3    V4    V5
       NG      -4.5   3.5   2.4  -0.5   5.5
       NG      -5.4   5.5   5.5   1.0   2.0  
       GL       2.0   1.5   -3.5  2.0   -5.5
       GL       3.5   6.5   -2.5  1.5   -2.5
       GL       4.5   1.5   -6.5  1.0   -2.0

以下是我的代码:

     library(dplyr)
     dataframe %>%
     group_by(Group) %>%
     summarize(COR=cor(V3,V4)) 

这是我的输出:

    Group       COR
    <chr>     <dbl>
      1    GL  0.1848529
      2    NG  0.1559912

我如何使用编辑此代码来获取p值?任何帮助,将不胜感激!我看过其他地方,但没有任何工作。谢谢!!

r dplyr correlation
1个回答
0
投票

如果你想看成对相关,你应该尝试?corrplot

library(corrplot)
df_cor <- cor(df[,sapply(df, is.numeric)])
corrplot(df_cor, method="color", type="upper", order="hclust")

在下图中,您可以注意到“正相关”以“蓝色”显示,“负相关”以“红色”显示,并且其强度与相关系数成比例。 output plot

#sample data
> dput(df)
structure(list(Group = structure(c(2L, 2L, 1L, 1L, 1L), .Label = c("GL", 
"NG"), class = "factor"), V1 = c(-4.5, -5.4, 2, 3.5, 4.5), V2 = c(3.5, 
5.5, 1.5, 6.5, 1.5), V3 = c(2.4, 5.5, -3.5, -2.5, -6.5), V4 = c(-0.5, 
1, 2, 1.5, 1), V5 = c(5.5, 2, -5.5, -2.5, -2)), .Names = c("Group", 
"V1", "V2", "V3", "V4", "V5"), class = "data.frame", row.names = c(NA, 
-5L))
© www.soinside.com 2019 - 2024. All rights reserved.