I want to calculate grid_metrics()
for several fields of my point cloud, using several functions. I want to perform this in loops to make my code look cleaner.
Basically I want this:
# loop through fields
for (name in c("field1", "field2", "field3")) {
# loop through statistics
for (type in list(c("mean", mean), c("sd", sd))) {
# calculate gris metrics
raster <- grid_metrics(point_cloud, ~type[[2]](name), res = 1)
However, this does not work. Using the statistic I specified works. However, whatever I tried, I did not manage to replace the field name with a variable containing the name. I always get the warning In mean.default(name) : argument is not numeric or logical: returning NA
.
I found a workaround using rasterize()
, but then these rasters have an offset to my other rasters created with grid_metrics()
and I think it would be better to create all rasters consistently in the same way.
(Here the workaround:)
raster <- rasterize(data.frame(X=point_cloud@data$X, Y=point_cloud@data$Y),
raster_object, field=get(name, point_cloud@data), fun = type[[2]])
Can someone help me to replace the column name with a variable? Putting an eval()
around the variable does not help either.