spearman correlation by group in R
How about this for a base R solution:
df <- data.frame(group = rep(c("G1", "G2"), each = 10),
var1 = rnorm(20),
var2 = rnorm(20))
r <- by(df, df$group, FUN = function(X) cor(X$var1, X$var2, method = "spearman"))
# df$group: G1
# [1] 0.4060606
# ------------------------------------------------------------
# df$group: G2
# [1] 0.1272727
And then, if you want the results in the form of a data.frame:
data.frame(group = dimnames(r)[[1]], corr = as.vector(r))
# group corr
# 1 G1 0.4060606
# 2 G2 0.1272727
EDIT: If you prefer a plyr
-based solution, here is one:
library(plyr)
ddply(df, .(group), summarise, "corr" = cor(var1, var2, method = "spearman"))
very old question, but this tidy
& broom
solution is extremely straightforward. Thus I have to share the approach:
set.seed(123)
df <- data.frame(group = rep(c("G1", "G2"), each = 10),
var1 = rnorm(20),
var2 = rnorm(20))
library(tidyverse)
library(broom)
df %>%
group_by(group) %>%
summarize(correlation = cor(var1, var2,, method = "sp"))
# A tibble: 2 x 2
group correlation
<fct> <dbl>
1 G1 -0.200
2 G2 0.0545
# with pvalues and further stats
df %>%
nest(-group) %>%
mutate(cor=map(data,~cor.test(.x$var1, .x$var2, method = "sp"))) %>%
mutate(tidied = map(cor, tidy)) %>%
unnest(tidied, .drop = T)
# A tibble: 2 x 6
group estimate statistic p.value method alternative
<fct> <dbl> <dbl> <dbl> <chr> <chr>
1 G1 -0.200 198 0.584 Spearman's rank correlation rho two.sided
2 G2 0.0545 156 0.892 Spearman's rank correlation rho two.sided
Since some time/dplyr
version, you need to write this to get results like above and no errors:
df %>%
nest(data = -group) %>%
mutate(cor=map(data,~cor.test(.x$var1, .x$var2, method = "sp"))) %>%
mutate(tidied = map(cor, tidy)) %>%
unnest(tidied) %>%
select(-data, -cor)