I'm trying to write my own function in R whose role is to automatically compute the correlation between genes and clinical features of interest. This is my code lines:
#Empty data.frame
cc1 <- data.frame(Estimate=paste("Site", 1:35), P.value="")
estimates = numeric(35)
pvalues = numeric(35)
#compute correlation between clinical feature and genes
computeCC = function(x)
{
if (x = ""){
for (i in 1:35) {
cc<-cor.test(cor[,i], cor[,x],
method = "spearman")
estimates[i] = cc$estimate
pvalues[i] = cc$p.value
cc1$Estimate <- estimates
cc1$P.value <- pvalues
rownames(cc1) = colnames(cor)[1:35]}}}
in which, cor is a data frame including 1904 patients and 38 columns (35 genes + "lymph", "npi" and "stage"); "lymph", "npi" and "stage" are column names in cor and are three clinical features, i.e., number of positive lymph nodes, Nottingham prognostic index and tumor stage, respectively.
I'm wanting to write a function so that when I write something like:
computeCC(lymph)
It will show me the correlation coefficient and p-value between numbers of lymph nodes and each of 35 gene.
Similarly, when I write: computeCC(stage)
It will show me the correlation coefficient and p-value between tumor stage and each of 35 gene.
But right now, I have been running into a problem:
Error: unexpected '}' in: " cc1$P.value <- pvalues rownames(cc1) = colnames(cor)[1:35]}}"
This is my reproducible data:
cor <- structure(list(NCOR1 = c(0.6488, 0.3312, -0.3336, 0.2663, -1.3986), ZFP36L1 = c(-1.4278, -1.9684, -1.4047, -1.1984, 0.397), SMAD4 = c(-0.5692, -2.5897, -1.4175, -2.2613, 0.6804), CDKN1B = c(-0.9829, -1.7246, -1.1409, -1.5033, -0.8475), CDH1 = c(-0.1387, 1.5924, -0.7637, 1.2737, 0.5298), PIK3R1 = c(0.2649, -0.2267, -0.6875, -0.8364, 1.3622), BRCA2 = c(0.6442, 1.2209, -0.6712, -1.0785, -0.296), KMT2C = c(-0.8759, -0.327, -0.0154, -0.7076, -0.0817), KRAS = c(0.5975, -0.0729, 0.0069, -1.3664, -0.9904), MUC16 = c(0.4375, -0.7318, -0.5569, -0.8224, -0.3882), CBFB = c(-0.9757, 0.9849, -0.9263, -1.7691, -0.7777), MAP2K4 = c(0.385, -0.6192, -1.5389, -0.1092, -2.4083), AHNAK2 = c(0.69, 0.2453, -0.0492, -1.0581, -0.2553), BAP1 = c(0.0535, -3.1571, 1.8473, -0.2338, -0.9715), ERBB2 = c(0.6171,4.4808, -0.643, 0.496, 1.1611), TP53 = c(-0.065, 1.3605, -0.0393, 1.6328, -0.3413), MAP3K1 = c(-1.241, -0.6619, -1.4874, -2.1246, 2.2862), ERBB3 = c(0.7237, -0.1072, -0.2926, -1.1115,0.5288), PTEN = c(-0.4454, -1.2554, -0.9175, -0.6936, -0.0996
), PIK3CA = c(-1.9252, -2.2674, -0.0451, -0.6883, -1.0361
), GPS2 = c(0.489, -0.363, 0.1914, -0.1519, 0.237), SF3B1 = c(1.0353,
1.0428, 0.1198, -0.1978, 1.3932), AGTR2 = c(0.395, 1.7066,
0.2963, 0.5277, 0.5876), SYNE1 = c(0.1814, -0.8717, -0.3925,
-0.6181, 0.2515), GATA3 = c(0.727, -0.1693, 0.1266, 0.2467,
0.7005), AKT1 = c(0.7579, 1.9675, -1.0293, -1.1985, -1.902
), FOXO3 = c(-0.1501, 0.0589, -0.3752, -0.4585, -0.8405),
ARID1A = c(0.7732, -0.695, 0.0034, -0.9322, 0.5824), RB1 = c(-0.135,
-0.6994, 0.487, 1.7919, 0.9048), CDKN2A = c(0.0647, 0.1072,
-0.3117, -0.2668, -0.6555), MEN1 = c(-0.5376, 2.164, 1.2287,
0.5037, 0.7852), NF1 = c(-0.5943, -0.2639, -0.8211, 0.2209,
1.5184), TBX3 = c(-0.765, -0.2696, 0.1784, 0.6917, 0.3603
), CHEK2 = c(-0.5534, 1.8462, -0.8928, 0.7362, -0.3503),
RUNX1 = c(-0.8007, -1.9473, 0.6226, -0.6965, 0.1434), lymph = c(1,
5, 8, 1, 0), npi = c(4.036, 6.032, 6.03, 5.042, 3.046), stage = c(2,
2, 3, 2, 2)), row.names = c("MB-0362", "MB-0346", "MB-0386", "MB-0574", "MB-0503"), class = "data.frame")
Can anyone suggest me an idea? Thanks in advance.
dput(head(df,n))
? – NelsonGondput()
for this. – MKR