Reputation: 396
Just corr.test, not cor or cor.test.
Here is my method,but I think it's a little complicated in some degree.
Can somebody give me a better or simple method to get the result with three cols named "Gene","r"(correlation) and "p value" as a dataframe ?
My sample below:
library(psych)
set.seed(7)
mean_data <- data.frame(
Name = c(paste0("Gene_", LETTERS[1:20])),
matx <- matrix(sample(1:1000, 1000, replace = T), nrow = 20)
)
names(mean_data)[-1] <- c(paste0("Sample_", 1:50))
rownames(mean_data) <- mean_data[,1]
mean_data <- mean_data[,-1]
tm <- corr.test(t(mean_data["Gene_A",,drop=FALSE]),
y = t(mean_data), use = "pairwise", "spearman", adjust="holm",
alpha=0.05, ci=F, minlength=5)
rr<-tm$r
rr<-t(rr)
colnames(rr)<-"Gene_A_r"
pp<-tm$p
pp<-t(pp)
colnames(pp)<-"Gene_A_P"
resu_r_p<-as.data.frame(bind_cols(list(rr=rr,pp=pp)),row.names = T)
rownames(resu_r_p)<-rownames(pp)
resu_r_p<-resu_r_p[-which(rownames(resu_r_p)== "Gene_A"),]
resu_r_p
resu_r_p<-resu_r_p[abs(resu_r_p$rr)>0.2&resu_r_p$pp<0.1,]
> resu_r_p
Gene_A_r Gene_A_P
Gene_B 0.080580100 1.000000000
Gene_C 0.031959852 1.000000000
Gene_D 0.234878863 1.000000000
Gene_E -0.184066462 1.000000000
Gene_F 0.038897426 1.000000000
Gene_G -0.116452171 1.000000000
Gene_H -0.339864573 0.283401669
Gene_I 0.067519869 1.000000000
Gene_J -0.051002521 1.000000000
Gene_K 0.525691510 0.001680064
Gene_L -0.008164049 1.000000000
Gene_M -0.002689205 1.000000000
Gene_N -0.266375336 1.000000000
Gene_O -0.004754130 1.000000000
Gene_P 0.071841909 1.000000000
Gene_Q -0.167210911 1.000000000
Gene_R -0.090138545 1.000000000
Gene_S 0.081974692 1.000000000
Gene_T 0.066317710 1.000000000
Upvotes: 1
Views: 234
Reputation: 72593
Looking for this?
res <-
setNames(as.data.frame(t(do.call(rbind, tm[c("r", "p")]))), paste0("Gene_A_", c("r", "p")))
res
# Gene_A_r Gene_A_p
# Gene_A 1.00000000 0.00000000
# Gene_B 0.01856254 1.00000000
# Gene_C -0.06934473 1.00000000
# Gene_D 0.01906454 1.00000000
# Gene_E 0.20562318 1.00000000
# Gene_F 0.01354174 1.00000000
# Gene_G 0.03265463 1.00000000
# Gene_H -0.06722850 1.00000000
# Gene_I 0.10850461 1.00000000
# Gene_J -0.07160180 1.00000000
# Gene_K -0.12298014 1.00000000
# Gene_L -0.01978439 1.00000000
# Gene_M -0.18954091 1.00000000
# Gene_N -0.39485185 0.08633315
# Gene_O -0.10450234 1.00000000
# Gene_P 0.10564733 1.00000000
# Gene_Q -0.18142528 1.00000000
# Gene_R 0.01335030 1.00000000
# Gene_S 0.10334478 1.00000000
# Gene_T 0.18810027 1.00000000
Upvotes: 1