【问题标题】:How do I calculate the mean of two different variables taking into account the values of latitude and longitude in R?考虑到R中的纬度和经度值,如何计算两个不同变量的平均值?
【发布时间】:2019-02-28 22:22:09
【问题描述】:

我目前正在尝试从表中获取 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

提前谢谢你

【问题讨论】:

  • 向我们展示一些数据和代码。

标签: r latitude-longitude bioinformatics mean biometrics


【解决方案1】:

您可以使用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)

【讨论】:

  • 我的理解是 Diego 需要一个二维分箱(分桶),每个分箱的平均值,而不是二维移动平均值?
  • 根据这句话:“这个变量对于所有可能的 5x5 网格单元的平均值。”我知道他需要移动平均线
  • 对不起,我认为我的解释不够好。但是,是的,@stenevang 是对的,我需要固定的 5x5 网格单元,每个单元有平均值。
  • 好的,我们可以使用raster::agregate进行5x5下采样。
  • 非常感谢,现在我得到了我需要的东西。
【解决方案2】:

这是一个使用 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) )

【讨论】:

  • 我试过你的脚本,但我得到了虚假的平均值,我认为这是由于脚本与我的数据集的 ANNUAL_RANGE 和 ANNUAL_MEAN 的原始值缺乏关联。你能帮我解决这个问题吗?提前坦克
  • 我的解决方案示例包含随机生成的虚拟值。您必须将解决方案应用于真实数据集。您应该获取您的数据集并从注释行“#defining band of 5...”等开始使用我的代码。这应该是一个很好的学习练习,可以快速掌握使用 R。
  • @DiegoCepeda 当然,这些值与原始值不对应。说它使用模拟数据的评论似乎很清楚这一点。如果您想使用您的数据,请从文件中读取。
  • 是的,我知道您的示例包含随机数据,我用自己的数据尝试了脚本,但我只是随机检查了一些网格单元,在 Excell 中进行计算,结果完全不同
  • 如果您与 Excel 进行比较,得到不同的结果,一种可能的解释是网格单元限制的选取方式不同。此外,您想要的是“固定”网格单元还是每个坐标的 5x5 移动平均值?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
相关资源
最近更新 更多