r - For the same longitude or latitude raster data use the function based on terra - Stack Overflow

Assuming that I need to calculate the variance of the raster data for the same longitude, Is using the

Assuming that I need to calculate the variance of the raster data for the same longitude, Is using the sapp function the right way to go? But I failed.

I can now use the more complex method of converting to data.frame and grouping to calculate the variance:

library(terra)
library(tidyverse)

r <- rast(nrows = 5, ncols = 10, vals = c(1:50))
plot(r)


r |> 
  terra::as.data.frame(xy= TRUE) |> 
  group_by(x) |> 
  summarise(lonsd = sd(lyr.1))

Is there a more straightforward function in terra? Please tell me if you know.

There are also more complex scenarios where this needs to be done on multiple layers of raster data at the same time, how should this be done?

Assuming that I need to calculate the variance of the raster data for the same longitude, Is using the sapp function the right way to go? But I failed.

I can now use the more complex method of converting to data.frame and grouping to calculate the variance:

library(terra)
library(tidyverse)

r <- rast(nrows = 5, ncols = 10, vals = c(1:50))
plot(r)


r |> 
  terra::as.data.frame(xy= TRUE) |> 
  group_by(x) |> 
  summarise(lonsd = sd(lyr.1))

Is there a more straightforward function in terra? Please tell me if you know.

There are also more complex scenarios where this needs to be done on multiple layers of raster data at the same time, how should this be done?

Share Improve this question asked Mar 23 at 8:55 BreezeBreeze 3347 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You can use aggregate to compute something for each column (or row) in a raster

Example data

library(terra)
set.seed(1)
r <- rast(nrows = 5, ncols = 10, vals = sample(100), nlyr=2)

Solution

a <- aggregate(r, c(nrow(r), 1), var, na.rm=TRUE)
data.frame(lon=xFromCol(r), values(a))
#    lon  lyr.1  lyr.2
#1  -162  468.2 1364.3
#2  -126 1007.3  493.0
#3   -90  557.5  822.2
#4   -54  358.3 1089.7
#5   -18 1224.3  665.7
#6    18 1125.3  260.3
#7    54 1180.3  207.2
#8    90  631.3  433.0
#9   126 1564.7 1669.7
#10  162  881.7  752.3

Or

a <- as.data.frame(r, xy=TRUE)
aggregate(a[,3:4], a["x"], var, na.rm=TRUE)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744292519a4567103.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信