简体   繁体   English

从纬度/经度坐标中减去数据

[英]Subtract data from lat/lon coordinates

I have 2 files of data that looks like this: 我有2个看起来像这样的数据文件:

Model Data
long  lat   count
96.25 18.75     4
78.75 21.25     3
86.75 23.25     7
91.25 33.75     10

Observation Data
long  lat   count
96.75 25.75    10
86.75 23.25     7
78.75 21.25    11
95.25 30.25     5

I'm trying to subtract the counts of the lat/long combinations (model data-observation data) that match such that the first combination of 78.75 & 21.25 would give a difference count of -8. 我试图减去匹配的经纬度组合(模型数据-观测数据)的计数,以使第一个组合78.75和21.25的差值为-8。 Any lat/long points without a match to subtract with would just be subtracted by or from 0. 任何不匹配要减去的经/纬度点都将被0减去或从中减去。

I've tried an if statement as such to match points for subtraction: 我已经尝试过if语句来匹配减法点:

if (modeldata$long == obsdata$long & modeldata$lat == obsdata$lat) {
      obsdata$difference <- modeldata$count - obsdata$count
 }

However, this just subtracts rows in order, not by matching points, unless matching points happen to fall within the same row. 但是,除非匹配点碰巧落在同一行内,否则这只会按顺序减去行,而不是按匹配点。

I also get these warnings: 我也收到以下警告:

Warning messages: 警告信息:

1: In modeldata$long == obsdata$long : longer object length is not a multiple of shorter object length 1:在modeldata $ long == obsdata $ long中:较长的对象长度不是较短的对象长度的倍数

2: In modeldata$lat == obsdata$lat : longer object length is not a multiple of shorter object length 2:在modeldata $ lat == obsdata $ lat中:较长的对象长度不是较短的对象长度的倍数

3: In if (modeldata$long == obsdata$long & modeldata$lat == : the condition has length > 1 and only the first element will be used 3:如果if(modeldata $ long == obsdata $ long&modeldata $ lat ==:条件的长度> 1,并且仅使用第一个元素

Any help would be greatly appreciated! 任何帮助将不胜感激!

You can merge on coordinates, add 0 for NA and substract. 您可以合并坐标,为NA加0并减去。

mdl <- read.table(text = "long  lat   count
96.25 18.75     4
78.75 21.25     3
86.75 23.25     7
91.25 33.75     10", header = TRUE)

obs <- read.table(text = "long  lat   count
96.75 25.75    10
                  86.75 23.25     7
                  78.75 21.25    11
                  95.25 30.25     5", header = TRUE)

xy <- merge(mdl, obs, by = c("long", "lat"), all.x = TRUE)

xy[is.na(xy)] <- 0

xy$diff <- xy$count.x - xy$count.y
xy

   long   lat count.x count.y diff
1 78.75 21.25       3      11   -8
2 86.75 23.25       7       7    0
3 91.25 33.75      10       0   10
4 96.25 18.75       4       0    4

You can do this using a data.table join & update 您可以使用data.table加入并更新

library(data.table)

## reading your supplied data
# dt_model <- fread(
#   'long  lat   count
# 96.25 18.75     4
#   78.75 21.25     3
#   86.75 23.25     7
#   91.25 33.75     10'
# )
# 
# 
# dt_obs <- fread(
# "long  lat   count
#   96.75 25.75    10
#   86.75 23.25     7
#   78.75 21.25    11
#   95.25 30.25     5"
# )

setDT(dt_model)
setDT(dt_obs)

## this join & update will update the `dt_model`. 
dt_model[ 
    dt_obs
    , on = c("long", "lat")
    , count := count - i.count
    ]

dt_model
#     long   lat count
# 1: 96.25 18.75     4
# 2: 78.75 21.25    -8
# 3: 86.75 23.25     0
# 4: 91.25 33.75    10

Noting the obvious caveat that joining on coordinates (floats/decimals) may not always give the right answer 注意一个明显的警告,即加入坐标(浮点数/小数)可能并不总是能给出正确的答案

Here is an option with dplyr 这是dplyr的选项

library(dplyr)
left_join(mdl, obs, by = c("long", "lat")) %>%
   transmute(long, lat, count = count.x - replace(count.y, is.na(count.y), 0))
#   long   lat count
#1 96.25 18.75     4
#2 78.75 21.25    -8
#3 86.75 23.25     0
#4 91.25 33.75    10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM