将自定义函数的多个参数传递给...在 R 中的聚合函数中
Pass multiple arguments for custom function to ... in aggregate function in R
我有一个自定义函数 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, ...)
}
这个函数应该对每个class(classes 1-17)应用一个权重,然后用模态函数计算一个聚合。
classes 及其权重存储在数据框中:
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")
目的是将此数据框的权重和 class 值传递给自定义聚合函数,以使其灵活(无硬编码权重)。此外,classes 的数量可能会有所不同。
示例数据可以这样生成:
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)
我不知道如何将权重和 class 值传递给自定义函数。我不清楚如何才能灵活地允许不同数量的 classes。 rep(x[x==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)
我有一个自定义函数 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, ...)
}
这个函数应该对每个class(classes 1-17)应用一个权重,然后用模态函数计算一个聚合。
classes 及其权重存储在数据框中:
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")
目的是将此数据框的权重和 class 值传递给自定义聚合函数,以使其灵活(无硬编码权重)。此外,classes 的数量可能会有所不同。
示例数据可以这样生成:
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)
我不知道如何将权重和 class 值传递给自定义函数。我不清楚如何才能灵活地允许不同数量的 classes。 rep(x[x==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)