Ker*_*rry 0 scripting for-loop r r-factor
在被推荐使用adehabitat来计算交叉口的数量后,我偶然发现了一个轻微(希望很简单)的问题.在这个库中我使用kerneloverlap命令,因为我需要计算交集的体积.我想知道你是否可以帮我解决一些编程问题.我需要修改脚本,使其"批量"处理友好.我知道R足以让自己陷入麻烦并失去头发,因为我知道某些事情应该是可能的,但却无法弄明白如何让它发挥作用.
命令很简单:
kerneloverlap(loc[,c("X","Y")], loc$year, lev = 90, grid=30, meth="VI", conditional=TRUE)
Run Code Online (Sandbox Code Playgroud)
从数据文件中取出x,y坐标的年份,并计算在90的利用率分布中网格单元格大小为30的交叉体积.
输入文件(见下文摘录)是anid,X,Y,year和seasons.对于这个例子,只有一个赛季(请记住我有3个赛季).对于这个例子,我想在每个单独的交叉量之间的1个季节之间进行比较.所以测试数据有2年1个季节和2个人.我想说的是"在2003至2004年的产犊季节,动物1的交叉口的体积为0.8,这表明高度重叠和对一个地点的保真度".
我还想比较季节.这使得动物1在其2003年夏季和越冬季节的交叉量为0.04,这表明重叠程度低且对该位置没有保真度.
需要注意的一点是:并非所有人每年都在场,或者每个赛季都活着.因此可能需要某种低级别.
到目前为止这是我的R脚本(它不起作用).请注意,输出也没有很好地连接在一起,我似乎无法获得编译文件.我喜欢它告诉我它与比较事物的年份,个人或季节.
IDNames= levels(loc$anid)
Year = unique(loc$year)
for (i in 1:(length(IDNames))){
vi90 = kerneloverlap(loc[,c("X","Y")], loc$year, lev = 90, grid=30, meth="VI", conditional=TRUE)
}
colnames(vi)= c(paste(IDNames[i],Year[n], sep =""),paste(IDNames[i], Year[n], sep =""))
}
write.csv(vi,"VolInter_indiv.csv")
structure(list(anid = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("c_002", "c_104"), class = "factor"), X = c(276646.0514,
276485.0397, 278102.4193, 278045.4716, 278993.8807, 274834.5677,
278516.0218, 296741.8328, 299080.2451, 291874.5068, 168540.0024,
168360.8211, 169538.2299, 164538.2592, 157321.7524, 148090.3478,
140575.2442, 133369.7162, 134375.0805, 138763.5342, 232347.5137,
231989.4609, 231793.1066, 234923.4012, 233374.4531, 232256.4667,
233660.3445, 239317.3128, 246354.664, 145161.8922, 144148.7895,
145154.7652, 145399.3515, 144581.4836, 143646.7295, 145055.3165,
144613.1393, 145037.3035, 144701.2676), Y = c(2217588.648, 2216616.387,
2219879.777, 2220818.804, 2216908.127, 2220423.322, 2216589.91,
2234167.287, 2239351.696, 2232338.072, 2273737.333, 2273954.782,
2269418.423, 2271308.607, 2264694.484, 2263710.512, 2254030.274,
2253352.426, 2248644.946, 2262359.026, 2231404.821, 2229583.89,
2231700.485, 2231598.882, 2237122.967, 2233302.185, 2240092.997,
2237702.817, 2249213.958, 2261841.308, 2263064.156, 2262236.452,
2264147.03, 2263214.877, 2263336.363, 2261417.946, 2256289.995,
2256694.953, 2253352.576), year = c(2003L, 2003L, 2003L, 2003L,
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L,
2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2004L, 2004L,
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L,
2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L), season = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L), .Label = "calving", class = "factor")), .Names = c("anid",
"X", "Y", "year", "season"), class = "data.frame", row.names = c(NA,
-39L))
Run Code Online (Sandbox Code Playgroud)
好的,我会咬人的.
你的代码有一些拼写错误(我希望),使其无法运行.让我们把它扔掉然后重新开始吧.该函数kerneloverlap为其第二个参数中指定的每对项返回重叠值矩阵.在你的第一个例子中,你要比较年份.
让我们首先想象一下我们对一只动物的数据做了什么,并编写一个函数来输出我们想要的值,只是为了这个简单的情况:
kernMod <- function(x){
#x is the data for a single animal
rs <- kerneloverlap(x[,c("X","Y")],
x$year,lev = 90,
grid = 30,
meth = "VI",
conditional = TRUE)
#Assumes we're only comparing two years
out <- data.frame(year = paste(colnames(rs),collapse="-"), val = rs[2,1])
out
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以分别将它应用于每只动物:
kernMod(subset(loc,anid == 'c_002'))
year val
1 2003-2004 0
> kernMod(subset(loc,anid == 'c_104'))
year val
1 2003-2004 0.06033966
Run Code Online (Sandbox Code Playgroud)
或者我们可以ddply从plyr包中使用它依次将它应用于每只动物:
ddply(loc,.(anid),.fun = kernMod)
anid year val
1 c_002 2003-2004 0.00000000
2 c_104 2003-2004 0.06033966
Run Code Online (Sandbox Code Playgroud)
要包含多个季节,您只需将其添加到要转换的变量列表中ddply(未经测试):
ddply(loc,.(anid,season),.fun = kernMod)
Run Code Online (Sandbox Code Playgroud)
要比较一年中的季节,你需要修改为第二个参数kernMod传递x$season,然后调用类似(未经测试):
ddply(loc,.(anid,year),.fun = kernMod)
Run Code Online (Sandbox Code Playgroud)
如果您的完整数据有多年,kernMod则需要进行一些修改,因为kerneloverlap返回一个nxn矩阵,其中n是数据中的年数.也许是这样的(未经测试)
kernMod <- function(x){
#x is the data for a single animal
rs <- kerneloverlap(x[,c("X","Y")],
x$year,lev = 90,
grid = 30,
meth = "VI",
conditional = TRUE)
rs[lower.tri(rs,diag = TRUE)] <- NA
rs <- melt(rs)
rs <- subset(rs, !is.na(value))
out <- data.frame(year = paste(rs$X1,rs$X2,collapse="-"), val = rs$value)
out
}
Run Code Online (Sandbox Code Playgroud)
这种方法应该通过仅计算您拥有数据的值来处理"缺失"的动物.
好.我很想得到第三至第四作者,但我愿意接受一个承认.;)