[R] Help in error removal

Mitra, Sumona sumona.mitra at kcl.ac.uk
Wed Jul 13 12:09:32 CEST 2011


Dear all,

Thank you so much for your help. I was able to get a script that ran and did what i wanted  it to do. It was as follows:-


> correlation <- function(a)
+ {
+   r <- matrix(nrow = dim(a)[1], ncol=dim(a)[1])
+   for(x in c(2:dim(a)[1]))
+   {
+     for(y in c(x:dim(a)[1]))
+     {
+       r[x,y] <- (cor.test(as.vector(as.matrix(a)[x,], mode="double"), as.vector(as.matrix(a)[y,], mode="double"))$p.value)
+       r[y,x] <- r[x,y]
+     }
+   }
+   return(r)
+ }

Now I want to extend this to include all the possible correlations that cor.test can do. One of the possible arguments of cor.test is method="method", where method can be pearson, spearman or kendall. To include this I modified my code in the following way:-

> correlation <- function(a, b)
+ {
+   r <- matrix(nrow = dim(a)[1], ncol=dim(a)[1])
+   for(x in c(2:dim(a)[1]))
+   {
+     for(y in c(x:dim(a)[1]))
+     {
+       r[x,y] <- (cor.test(as.vector(as.matrix(a)[x,], mode="double"), as.vector(as.matrix(a)[y,], mode="double"), method="b")$p.value)
+       r[y,x] <- r[x,y]
+     }
+   }
+   return(r)
+ }

This when executed though, gives the following error:-

> correlation(a, spearman)
Error in match.arg(method) : 
  'arg' should be one of “pearson”, “kendall”, “spearman”

Please help!

Sumona 

 


More information about the R-help mailing list