如何考虑R中的纬度和经度值来计算两个不同变量的平均值?

时间:2018-09-25 10:13:59

标签: r latitude-longitude bioinformatics mean biometrics

我当前正在尝试从表中获取R中的一些数据。

我有一个数据集,其中包含两个不同的变量,即全球海表温度(SST)的年范围和年平均值。对于每个纬度(从90到-90)和经度(从180到-180),我都有这些值。

我想获得5x5纬度/经度网格单元的上述变量的平均值(年度范围和年度平均值)。例如,我将需要知道-180和-176之间的经度以及90和86之间的纬度的“年度范围”平均值,依此类推,直到获得所有可能的5x5网格单元的该变量的平均值。

我的数据如下:

lon lat ANNUAL_MEAN ANNUAL_RANGE 1 0.5 89.5 -1.8 0 2 1.5 89.5 -1.8 0 3 2.5 89.5 -1.8 0 4 3.5 89.5 -1.8 0 5 4.5 89.5 -1.8 0 6 5.5 89.5 -1.8 0 ... 52001 354.5 -89.5 -1.8 0 52002 355.5 -89.5 -1.8 0 52003 356.5 -89.5 -1.8 0 52004 357.5 -89.5 -1.8 0 52005 358.5 -89.5 -1.8 0 52006 359.5 -89.5 -1.8 0

提前谢谢

2 个答案:

答案 0 :(得分:3)

您可以使用raster程序包及其focal函数通过移动窗口进行计算。

首先,我将创建一个虚拟data.frame来代表您的数据

# Prepare dummy data.frame
set.seed(2222)
lonlat <- expand.grid(1:10, 1:10)
df <- data.frame( lon = lonlat[, 1],
                  lat = lonlat[, 2],
                  ANNUAL_MEAN = rnorm(100),
                  ANNUAL_RANGE = runif(100, 1, 5)
                )

现在,我们必须将数据帧转换为栅格并执行移动窗口平均。

library(raster)

# Convert data frame to raster object
rdf <- df
coordinates(rdf) <- ~ lon + lat
gridded(rdf) <- TRUE
rdf <- brick(rdf) # our raster brick

## Perform moving window averaging

# prepare weights matrix (5*5)
w <- matrix(1, ncol = 5, nrow = 5)

# perform moving window averaging
ANNUAL_MEAN_AVG <- focal(rdf[[1]], w, mean, pad = TRUE, na.rm = TRUE)
ANNUAL_RANGE_AVG <- focal(rdf[[2]], w, mean, pad = TRUE, na.rm = TRUE)

# Append new data to initial data.frame
df$ANNUAL_MEAN_AVG <- as.data.frame(ANNUAL_MEAN_AVG)
df$ANNUAL_RANGE_AVG <- as.data.frame(ANNUAL_RANGE_AVG)

现在df$ANNUAL_MEAN_AVGdf$ANNUAL_RANGE_AVG中的每个单元格都包含相应的5 * 5平方的平均值。

UPD 1. 5x5下采样

如果您需要一个固定的5x5网格单元(每个单元的平均值),则可以使用raster::agregate函数。

使用上一个示例中的rdf光栅砖。

# perform an aggregation with given downsampling factor
rdf_d <- aggregate(rdf, fact=5, fun = mean)

# Now each pixel in the raster `rdf_d` contains a mean value of 5x5 pixels from initial `rdf`
# we need to get pixels coordinates and their values
coord <- coordinates(rdf_d)
vals <- as.data.frame(rdf_d)
colnames(coord) <- c("lon", "lat")
colnames(vals) <- c("ANNUAL_MEAN_AVG", "ANNUAL_RANGE_AVG")

res <- cbind(coord, vals)

答案 1 :(得分:1)

这是使用tidyverse中包含的dplyr软件包的解决方案。应该很容易逐步进行。

library(tidyverse)

# set.seed() assures reproducability of the example with identical random numbers
set.seed(42)


# build a simulated data set as described in the question
lats <- seq(from = -90, to = 90, by = 0.5)
lons <- seq(from = -180, to = 179.5, by = 0.5) # we must omit +180 or we would
                                               # double count those points
                                               # since they coincide with -180

    # combining each latitude point with each longitude point
coord <- merge(lats, lons) %>%
    rename(lat = x) %>% 
    rename(lon = y) %>%
    # adding simulated values
    mutate(annual_mean = runif(n = nrow(.), min = -2, max = 2)) %>%
    mutate(annual_range = runif(n = nrow(.), min = 0, max = 3)) %>% 
    # defining bands of 5 latitude and 5 longitude points by using integer division
    mutate(lat_band = lat%/%5) %>% 
    mutate(lon_band = lon%/%5) %>% 
    # creating a name label for each unique 5x5 gridcell
    mutate(gridcell_5x5 = paste(lat_band, lon_band, sep = ",")) %>%
    # group-by instruction, much like in SQL
    group_by(lat_band, lon_band, gridcell_5x5) %>% 
    # sorting to get a nice order
    arrange(lat_band, lon_band) %>% 
    # calculating minimum and maximum latitude and longitude for each gridcell
    # calculating the mean values per gridcell
    summarize(gridcell_min_lat = min(lat), 
              gridcell_max_lat = max(lat),
              gridcell_min_lon = min(lon),
              gridcell_max_lon = max(lon),
              gridcell_mean_annual_mean = round(mean(annual_mean), 3),
              gridcell_mean_annual_range = round(mean(annual_range), 3) )