Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

r - Getting p values for groupwise correlation using the dplyr package

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

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

Following is my code:

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

Here is my output:

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

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!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...