如何比较两个不同的文件与坐标,并将公共坐标和新坐标写入新文件?

时间:2015-03-31 09:23:27

标签: r

我正在尝试使用坐标比较两个不同的文件,并将公共坐标和新/旧坐标写入新文件。我其实相信 - 操作员会工作!但是我又是R的新手,这是一个菜鸟的错误。 加法和删除是带坐标的文件。而且我不知道该怎么办。我按照我在这里提供的代码进行了for循环。但我选择删除它,以便我看起来不像实际上我是一个白痴:)我试图将比较中的公共坐标作为“常见”和其他坐标,而不管删除的公共坐标。 csv到'新坐标',并且像从adder.csv到'旧坐标'的其余坐标一样明智。请指教..

library(plyr)

adder <- read.csv(file="//eudrrs09/common/99_User/99_schallj/_tmp/adder1.csv",header=TRUE, sep = ";", fill = TRUE)
adder1 <- data.frame("x"=adder$X, "y"=adder$Y)

removed <- read.csv(file="//eudrrs09/common/99_User/99_schallj/_tmp/removed1.csv",header=TRUE, sep = ";", fill = TRUE)
removed1 <- data.frame("x"=removed$X, "y"=removed$Y)

z <- adder1 - removed1
count(z, c("x", "y"))

1 个答案:

答案 0 :(得分:0)

为了展示一种方法,我在加法器对象中创建了一组模拟坐标(美国怀俄明州的城市)。

dput(adder)
structure(list(V1 = c(42.06, 44.63, 44.08, 41.41, 42.66, 44.53, 
41.94, 41.18, 39.02), V2 = c(-104.18, -104.18, -104.13, -104.1, 
-104.09, -104.08, -104.07, -104.06, -84.56), interact = c("42.06-104.18", 
"44.63-104.18", "44.08-104.13", "41.41-104.1", "42.66-104.09", 
"44.53-104.08", "41.94-104.07", "41.18-104.06", "39.02-84.56"
)), .Names = c("V1", "V2", "interact"), row.names = c(NA, -9L
), class = "data.frame")

Then I used the same coordinates of adder plus a handful of new ones, in removed.

dput(removed)
structure(list(V1 = c(41.91, 41.53, 41.79, 43.84, 42.06, 44.63, 
44.08, 41.41, 42.66, 44.53, 41.94, 41.18, 39.02), V2 = c(-104.29, 
-104.27, -104.27, -104.21, -104.18, -104.18, -104.13, -104.1, 
-104.09, -104.08, -104.07, -104.06, -84.56), interact = c("41.91-104.29", 
"41.53-104.27", "41.79-104.27", "43.84-104.21", "42.06-104.18", 
"44.63-104.18", "44.08-104.13", "41.41-104.1", "42.66-104.09", 
"44.53-104.08", "41.94-104.07", "41.18-104.06", "39.02-84.56"
)), .Names = c("V1", "V2", "interact"), row.names = c(NA, -13L
), class = "data.frame")

下一步很简单:将坐标粘贴到一个新变量中。

adder$interact <- paste(adder$V1, adder$V2, sep = "")
removed$interact <- paste(removed$V1, removed$V2, sep = "")

adder
         V1      V2     interact
    1 42.06 -104.18 42.06-104.18
    2 44.63 -104.18 44.63-104.18
    3 44.08 -104.13 44.08-104.13
    4 41.41 -104.10  41.41-104.1
    5 42.66 -104.09 42.66-104.09
    6 44.53 -104.08 44.53-104.08
    7 41.94 -104.07 41.94-104.07
    8 41.18 -104.06 41.18-104.06
    9 39.02  -84.56  39.02-84.56

最后,使用setdiff来查找不匹配的坐标。如果需要,您可以稍后拆分它们。

new.coordinates <- setdiff(removed$interact, adder$interact)
new.coordinates
[1] "41.91-104.29" "41.53-104.27" "41.79-104.27" "43.84-104.21"