本文介绍了使用dplyr包获取p值以进行分组相关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在数据框中的某些变量之间运行关联.我有一个字符向量(组),其余为数字.

I am trying run correlations between some variables in a dataframe. I have one character vector (group) and rest are numeric.

dataframe<-

dataframe<-

       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值?任何帮助,将不胜感激!我在其他地方看过,但是什么也没用.谢谢!

How do i use edit this code to get the p-values? Any help would be appreciated! I have looked elsewhere but nothing is working. Thanks!!

推荐答案

如果要查看成对相关性,应该尝试?corrplot

You should try ?corrplot if you want to see pairwise correlation

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

在下图中,您可以注意到正相关"以蓝色"显示,负相关"以红色"颜色显示,并且强度与相关系数成正比.

In below graph you can notice that 'positive correlations' are displayed in 'blue' and 'negative correlations' in 'red' color and it's intensity are proportional to the correlation coefficients.

#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))

这篇关于使用dplyr包获取p值以进行分组相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 09:54