简体   繁体   中英

r dataframe correlation coefficient and p value

I have a dataframe df with columns

Parameter  A  B

I want to get the pearson correlation coeeficient and the corresponding p value as two columns appended to the above, so the result will look like

Parameter A B corr_coeff p-value

I tried

ddply(df, .(Parameter), summarise, "corr_coeff" = cor(A, B, method = "pearson"))

but can get only the correlation coefficient.Please let me know of a solution

An option would be cor.test

library(dplyr)
library(tidyr)
df %>%
   group_by(Parameter) %>%
   summarise(out = list(tibble(cor.test(A, B)[c("statistic", "p.value")]))) %>%
   unnest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM