【问题标题】:Pass multiple arguments for custom function to ... in aggregate function in R将自定义函数的多个参数传递给...在 R 中的聚合函数中
【发布时间】:2019-09-03 23:59:42
【问题描述】:

我有一个自定义函数f 用栅格包的聚合函数聚合一个栅格。该函数应该接受多个参数,例如参数second_argument.f 是这样构造的:

f <- function(x, ...) {

 

aggregate(r, fact=10, fun=f, second_argument = 2)

但是,当我想传递第二个参数时,这会失败。 从集成函数(如 aggregate())传递附加参数的正确方法是什么?

编辑

这是我的自定义聚合函数:

weighted_aggregation <- function(x, ...) {
  y <- c(
    rep(x[x==1], 1),
    rep(x[x==2], 5),
    rep(x[x==3], 5),
    rep(x[x==4], 5),
    rep(x[x==5], 1),
    rep(x[x==6], 1),
    rep(x[x==7], 3),
    rep(x[x==8], 5),
    rep(x[x==9], 5),
    rep(x[x==10], 5),
    rep(x[x==11], 2),
    rep(x[x==12], 3),
    rep(x[x==13], 1),
    rep(x[x==14], 1),
    rep(x[x==15], 1),
    rep(x[x==16], 5),
    rep(x[x==17], 5)
  )
  modal(y, ...)
}

这个函数应该对每个类(1-17类)应用一个权重,然后用模态函数计算一个聚合。

类及其权重存储在数据框中:

structure(list(clutter_class = c("Forest", "suburban_low_building", 
"rural_building", "residential_building", "Snow", "Water", "Road", 
"dense_urban_high_building", "urban_high_building", "dense_urban_low_building", 
"Railway", "Highway", "Open Urban", "Open Rural", "Rock", "suburban_high_building", 
"urban_low_building"), weight = c("1", "5", "5", "5", "1", "1", 
"3", "5", "5", "5", "2", "3", "1", "1", "1", "5", "5"), clutter_code = c("1", 
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", 
"14", "15", "16", "17")), row.names = c(NA, 17L), class = "data.frame")

目的是将此数据帧的权重和类值传递给自定义聚合函数,使其灵活(无硬编码权重)。此外,数字类别可能会有所不同。

样本数据可以这样生成:

r <- raster(res=5)
values(r) <- sample(c(1:17,2,3,3), ncell(r), replace=TRUE)

然后我会调用 aggregate() 函数:

m <- aggregate(r, fact=20, fun=weighted_aggregation)

我不知道如何将权重和类值传递给自定义函数。我不清楚如何使其灵活地允许不同数量的课程。不知何故,rep(x[x==1] 部分应该是动态创建的。

非常感谢您的帮助!

【问题讨论】:

  • 好的,谢谢,但我需要这个用于raster::aggregate 功能,所以dplyrdata.table 不是一个选项。我认为应该可以以某种方式传递其他参数?
  • 抱歉好像我读你的问题太快了,你是对的dplyrdata.table 对你没用。是的,我很确定您可以使用 raster::aggregate 函数传递其他参数,因为无论如何都存在 ... 参数。你遇到了什么错误?
  • aggregate(r, fact=10, fun = function(x) f(x, 2))
  • 谢谢,但这会导致Error in FUN(newX[, i], ...) : unused argument (na.rm = na.rm)

标签: r function aggregate raster


【解决方案1】:

不,你不能那样做,这很遗憾,但话说回来,你确实需要那样做,因为你可以将参数写入你的函数(作为常量),或者让函数在全局中找到它环境。

这是你的函数,为了灵活性而重写

w_agg <- function(x, na.rm=TRUE, ...) {
    weight <- c(1, 5, 5, 5, 1, 1, 3, 5, 5, 5, 2, 3, 1, 1, 1, 5, 5)
    i <- is.na(x)
    if ((!na.rm) | all(i)) {
        return(NA)
    }
    x <- x[!i]
    y <- rep(x, weight[x])
    # to get the same results as with your function you need
    # sort, such that the ties are treated the same way
    # y <- sort(y)
    modal(y)
}

library(raster)
r <- raster(res=5)
values(r) <- sample(17, ncell(r), replace=TRUE)
m <- aggregate(r, fact=10, fun=w_agg)

通常,您会将weight 设为w_agg 的参数,但要将其与不同的权重合计使用,您必须执行以下操作:

w_agg2 <- function(x, na.rm=TRUE, ...) {
    i <- is.na(x)
    if ((!na.rm) | all(i)) {
        return(NA)
    }
    x <- x[!i]
    y <- rep(x, weight[x])
    modal(y)
}

# create another example weights vector 
weight = rev(c(1, 5, 5, 5, 1, 1, 3, 5, 5, 5, 2, 3, 1, 1, 1, 5, 5))
m <- aggregate(r, fact=10, fun=w_agg2)

【讨论】:

  • 好的,谢谢 - 它对应于这个问题:stackoverflow.com/questions/56989358/… 我想通过参数传递权重。理想情况下,我可以将具有类/权重组合的 data.frame 传递给自定义函数。
  • 如果我像 f(x, y) 这样在自定义函数中使用常量参数,我将如何从 aggregate() 传递参数的值?
  • 请编辑您的问题。添加一些示例数据,并添加一个您想使用的工作函数示例。
  • 好的,罗伯特,我编辑了这个问题,希望对您有所帮助。抱歉不清楚。
  • 很好——除了为什么在权重向量上是rev()?当我不反转权重向量时,我可以重现结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 2019-03-29
相关资源
最近更新 更多