我正在尝试在循环中使用cor.test()
r函数,但我无法完全将其设置为"函数"。我可以单独调用这些项目,但我更喜欢使用循环。
我的代码选择如下所示:
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')}
}
这不起作用(没有输出),但是,这确实:
cor.test(mydata$Bacteroidetes, mydata$Actinobacteria, method = 'spearman')
我已经尝试了循环的几种变体,但我一直得到一个错误,说"x"(或"y")必须是数字向量。
我的数据看起来像这样(打印(我的数据))
PHYLUM Actino Bacter ...Tenericutes
x1 25 45 ...8
x1 26 42 ...8
x2 40 43 ...7
x2 42 41 ...5
x2 40 41 ...5
或以其原始格式:
PHYLUM,Actinobacteria,Bacteroidetes,...Tenericutes
x1,25,45,...8
x1,26,42,...8
x2,40,43,...7
x2,42,41,...5
x2,40,41,...5
我做错了什么,我该如何更改它,因为代码会产生一些输出?
谢谢你。
用打印将你的调用包围起来。