修改r中批量处理的代码

发布于 2024-12-06 00:56:43 字数 1437 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

驱逐舰岛风号 2024-12-13 00:56:43

好吧,我会咬的。

您的代码有一些拼写错误(我希望)导致它无法运行。让我们把它扔掉并重新开始。函数 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
}

现在我们可以将其单独应用于每只动物:

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

或者我们可以使用 ddply 来自 plyr 包,依次将其应用到每种动物:

ddply(loc,.(anid),.fun = kernMod)

  anid      year        val
1 c_002 2003-2004 0.00000000
2 c_104 2003-2004 0.06033966

要包含多个季节,您只需将其添加到变量列表中即可在 ddply 中进行拆分code>(未经测试):

ddply(loc,.(anid,season),.fun = kernMod)

比较一年内的季节之间,您需要修改 kernMod 以传递 x$season 作为第二个参数,然后调用类似的内容(未经测试):

ddply(loc,.(anid,year),.fun = kernMod)

如果您的完整数据有多个年,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
}

这种方法应该通过仅计算您有数据的值来处理“丢失”的动物。

好的。我很想为此获得第三至第四作者,但我会满足于承认。 ;)

Ok, I'll bite.

Your code had some typos (I hope) that make it un-runnable. Let's throw it out and start over. The function kerneloverlap returns a matrix of overlap values for each pair of items specified in its second parameter. In your first example you're comparing years.

Let's start by imagining what we'd do with the data on just one animal, and write a function that outputs the values we want just for that simple case:

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
}

Now we can apply this to each animal separately:

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

or we can use ddply from the plyr package to apply it to each animal in turn:

ddply(loc,.(anid),.fun = kernMod)

  anid      year        val
1 c_002 2003-2004 0.00000000
2 c_104 2003-2004 0.06033966

To include multiple seasons, you would simply add that to the list of variables to split over in ddply (untested):

ddply(loc,.(anid,season),.fun = kernMod)

To compare between seasons within year, you'll need to modify kernMod to pass x$season as the second argument and then call something like (untested):

ddply(loc,.(anid,year),.fun = kernMod)

If your full data has multiple years in it, kernMod will require some more modification, since kerneloverlap returns an n x n matrix, where n is the number of years in your data. Perhaps something like this (untested)

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
}

This approach should handle "missing" animals by only calculating values for which you have data.

Ok. I'd love to get 3rd-4th author for this, but I'd settle for an acknowledgement. ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文