Wednesday, 11 September 2013

No output from loop calling r cor.test()

No output from loop calling r cor.test()

I am trying to use the cor.test() r function in a loop and I cannot quite
get it to 'function'. I can call the items individually, but I would
prefer to use a loop.
A selection of my code looks like this:
hNames= scan(fileName, nlines = 1, what = character(), sep = ',')
mydata = read.table(fileName, header = TRUE, sep = ',')
names = c(hNames[2:length(hNames)])
for (i in names(mydata[2:length(mydata)]))
{
for (j in names(mydata[2:length(mydata)]))
{cor.test(mydata[[i]], mydata[[j]], method='spearman')}
}
Which does not work (there is no output), however, this does:
cor.test(mydata$Bacteroidetes, mydata$Actinobacteria, method = 'spearman')
I have tried several variations on the loop, but I have consistently
gotten an error that says 'x' (or 'y') must be a numeric vector.
My data looks like this (print(mydata))
PHYLUM Actino Bacter ...Tenericutes
x1 25 45 ...8
x1 26 42 ...8
x2 40 43 ...7
x2 42 41 ...5
x2 40 41 ...5
Or in its original format:
PHYLUM,Actinobacteria,Bacteroidetes,...Tenericutes
x1,25,45,...8
x1,26,42,...8
x2,40,43,...7
x2,42,41,...5
x2,40,41,...5
What am I doing incorrectly and how can I change it s.t. the code produces
some output?
Thank you kindly.

No comments:

Post a Comment