admin管理员组

文章数量:1122832

Assume I have a spatial temporal dataframe which looks like:

# Load necessary libraries
library(sf)
library(dplyr)

# Create a simple spatial polygon grid
polygons <- st_make_grid(
  st_bbox(c(xmin = 0, ymin = 0, xmax = 10, ymax = 10)), 
  cellsize = c(2, 2),
  square = TRUE
) %>%
  st_as_sf() %>%
  mutate(poly_id = row_number())  # Add polygon identifiers

# Create a temporal variable with years
years <- seq(2000, 2005)

# Expand grid to simulate spatiotemporal data
spatiotemporal_data <- expand.grid(
  poly_id = polygons$poly_id,
  year = years
) %>%
  left_join(polygons, by = "poly_id") %>%
  st_as_sf()

# Add a variable with some NA values
set.seed(42)  # For reproducibility
spatiotemporal_data <- spatiotemporal_data %>%
  mutate(variable = ifelse(runif(n()) > 0.8, NA, rnorm(n())),
         variable = if_else(poly_id == 3, NA, variable))

Now I would like first: do an temporal inter-/ extrapoliation within a poly_id. However I'm able to do run the temporal interpolarization by:

patiotemporal_data <- spatiotemporal_data %>%
  arrange(poly_id, year) %>% 
  group_by(poly_id) %>% 
         mutate(across(variable, ~ ifelse(any(!is.na(.)), zoo::na.approx(., na.rm = TRUE, rule = 2),. ))) %>% 
  ungroup()

Now I want to fill the remaing NAs (in Group 3, as no variable was given) by spatial interpolariation.

Approaches I found yet, using e.g. point to interpolate on a grid. But what if I just want to fill the NAs? thanks.

本文标签: rspatial temporal interpolarizationStack Overflow