0

我有两个数据框如下:

PickUP <- data.frame(pickuplong = c(-73.93909 ,-73.94189 ,-73.93754,-73.91638,-73.92792 ,-73.88634), pickuplat =c(40.84408,40.83841,40.85311,40.84966,40.86284,40.85628)) 

Dropoff <- data.frame(pickuplong = c(-73.93351 ,-73.93909 ,-73.93909 ,-73.80747,-73.95722,-73.91880), pickuplat =c(40.76621,40.84408,40.85311,40.69951,40.68877,40.75917), Droplong =c(-73.91300,-73.96259 ,-73.94870,-73.93860,-73.93633, -73.90690), Droplat =c(40.77777,40.77488 ,40.78493,40.84463,40.75977,40.77013)) 

我尝试在dropoff dataframe中重复的拾取数据帧中找到拾取坐标(经度和纬度) 。我有下面的代码,但我得到了这个错误:

library(sp)
library(rgdal)
library(leaflet)
library(mapview)
library(dplyr)
a <- semi_join(Dropoff , PickUP , by = c("pickuplong","pickuplat"))
a$ID <- 1:nrow(a) 
Dropoff_p <- a[, c("ID", "Pickup_longitude", "Pickup_latitude")]
Dropoff_d <- a[, c("ID", "Dropoff_longitude", "Dropoff_latitude")]
coordinates(Dropoff_p) <- ~Pickup_longitude + Pickup_latitude
coordinates(Dropoff_d) <- ~Dropoff_longitude + Dropoff_latitude
proj4string(Dropoff_p) <- CRS("+init=epsg:4326")
proj4string(Dropoff_d) <- CRS("+init=epsg:4326")
map_p <- mapview(Dropoff_p, color = "red")
map_d <- mapview(Dropoff_d, color = "blue")
map_p + map_d

我的错误是:

$<-.data.frame ( tmp , "ID", value = c(1L, 0L)) 中的错误:替换有 2 行,数据有 0 总结期间错误:无法打开连接

4

1 回答 1

0

在对数据框进行子集化时,您必须使用相同的列名。Dropoff_p我更改了、Dropoff_dcoordinates(Dropoff_p)和中的列名proj4string(Dropoff_d),然后您的脚本就可以工作了。

此外,该mapview软件包刚刚有一个新的更新。如果你愿意,你可以更新你mapview的版本2.0.1。您也可以添加col.regions = "red"col.regions = "blue"因为似乎在新版本下该color参数只会改变一个点的轮廓。要更改填充颜色,请使用col.regions.

library(sp)
library(rgdal)
library(leaflet)
library(mapview)
library(dplyr)

a <- semi_join(Dropoff , PickUP , by = c("pickuplong","pickuplat"))
a$ID <- 1:nrow(a) 
Dropoff_p <- a[, c("ID", "pickuplong", "pickuplat")]
Dropoff_d <- a[, c("ID", "Droplong", "Droplat")]
coordinates(Dropoff_p) <- ~pickuplong + pickuplat
coordinates(Dropoff_d) <- ~Droplong + Droplat
proj4string(Dropoff_p) <- CRS("+init=epsg:4326")
proj4string(Dropoff_d) <- CRS("+init=epsg:4326")
map_p <- mapview(Dropoff_p, color = "red", col.regions = "red")
map_d <- mapview(Dropoff_d, color = "blue", col.regions = "blue")
map_p + map_d
于 2017-05-08T13:45:02.067 回答