In R, I need to put the results of a correlation analysis between two variables in a table.csv file. The original dataframe is made up of three columns: the first column (group) contains the group to which the observations belongs, while the other two columns (var1 and var2) contains the values of the two variables.
df <- data.frame(group = rep(c("G1", "G2"), each = 10),
var1 = rnorm(20),
var2 = rnorm(20))
I conducted the correlation analysis by group.
spear <- by(df, df$group, FUN = function(X) cor.test(X$var1, X$var2, method = "spearman"))
And I got this output:
spear
#df$group: G1
# Spearman's rank correlation rho
#data: X$var1 and X$var2
#S = 144, p-value = 0.7329
#alternative hypothesis: true rho is not equal to 0
#sample estimates:
# rho
#0.1272727
#---------------------------------------------------------------------------------------
#df$group: G2
# Spearman's rank correlation rho
#data: X$var1 and X$var2
#S = 122, p-value = 0.4697
#alternative hypothesis: true rho is not equal to 0
#sample estimates:
# rho
#0.2606061
Now I need to obtain a table, with the first row as header, in csv format, with the id of the group in the first column, the rho value for that group in the second column, and the p-value for that group in the third column. The table should look like this:
group,rho,pvalue
G1,0.1272727,0.7329
G2,0.2606061,0.4697
You can create a dataframe in your by
function extracting the values that you want.
result <- do.call(rbind, by(df, df$group, FUN = function(x) {
tmp <- cor.test(x$var1, x$var2, method = "spearman")
data.frame(group = x$group[1], rho = tmp$estimate, p.value = tmp$p.value)
}))
result
# group rho p.value
#G1 G1 -0.261 0.470
#G2 G2 -0.442 0.204
To write the data to csv we can use write.csv
:
write.csv(result, 'result.csv', row.names = FALSE)