我正在调查包含 20 个特征变量的数据集中是否存在收敛进化。我需要单独测试每个特征的收敛性,我试图建立一个循环来做到这一点。但是,我的循环出错了,我不知道为什么。我可以完美地在函数中独立运行每个变量。
我正在使用包windex 并使用函数test.windex。该函数使用一个 data.frame ,其中包含有关哪些物种是焦点物种以及人们希望测试收敛的特征的信息。该函数还需要 data.frame 中包含的物种的系统发育树。
为了创建一个可重现的示例,我使用了随包 windex 提供的示例数据,因为我的数据是机密的(我已经运行了以下代码并得到了与我自己的数据相同的错误)。
data(sample.data) #data.frame containing the focal species and variables
data(sample.tree) #phylogentic tree
head(sample.data)
species focals bm1 bm2 bm3
1 s4 0 13.03895 17.2201554 11.43644
2 s7 0 18.22705 15.2427947 22.75245
3 s8 0 12.38588 10.5858736 13.80216
4 s9 0 24.79114 8.1148456 23.38717
5 s11 1 28.20126 -2.9911114 15.63215
6 s12 1 29.45775 0.9225023 12.09184
ou1 ou2 ou3 bin
1 13.03895 17.220155 11.43644 1
2 18.22705 15.242795 22.75245 2
3 12.38588 10.585874 13.80216 3
4 24.79114 8.114846 23.38717 4
5 21.22215 21.936316 20.30037 5
6 21.24501 20.952824 20.88650 6
#My loop, it is the traits that I need to loop for each trait variable in the sample.data
sapply(sample.data[3:8], function(x) test.windex(sample.data,sample.tree,traits= x,
focal=sample.data[,2], reps=1000,plot=TRUE,col="light grey"))
此代码产生错误:Error in .subset(x, j) : only 0's may be mixed with negative subscripts
调用自:[.data.frame
(dat, , traits)
我假设这是某种索引问题,但我似乎无法找到解决方法。我尝试了这个,但也出现了同样的错误:
traitsonly<-sample.data[3:8]
sapply(traitsonly, function(x) test.windex(sample.data,sample.tree,traits= x,
focal=sample.data[,2], reps=1000,plot=TRUE,col="light grey"))
任何帮助将不胜感激。
答案 0 :(得分:0)
从 test.windex
的帮助页面,它接受列号或列名。
特征 要计算 Wheatsheaf 指数的性状的列号(或名称)。
所以其中任何一个都应该有效。
library(windex)
sapply(3:8, function(x)
test.windex(sample.data,sample.tree,traits= x,
focal=sample.data[,2], reps=1000,plot=TRUE,col="light grey")
) -> plot
或
sapply(names(sample.data)[3:8], function(x)
test.windex(sample.data,sample.tree,traits= x,
focal=sample.data[,2], reps=1000,plot=TRUE,col="light grey")
) -> plot