我想在data.table的每一行上运行distVincentyEllipsoid()函数。可悲的是,它似乎没有矢量化:
library(geosphere)
mydat[, dest_home_orig := round(distVincentyEllipsoid(c(zip_long, zip_lat), c(orig_long, orig_lat))/1609.34,1)]`
试过:
mydat2[,.(round(distVincentyEllipsoid(c(zip_long, zip_lat), c(orig_long, orig_lat))/1609.34,1)), by=.I]
.pointsToMatrix(p1)中的错误:向量的长度错误,应为2
解决方法,使用for循环(!)。我想要一个data.table解决方案,如果存在的话。我已经看到了其他答案,但它们涉及到联接。这应该是一个直接的表内操作。
下面的输入:
mydat <- structure(list(zip_long = c(-65.9, -65.9, -72.52, -72.68, -72.59
), zip_lat = c(18.37, 18.37, 42.37, 42.26, 42.1), orig_long = c(-117.18972222,
-121.92916667, -122.31166667, -87.90805556, -118.40722222), orig_lat = c(32.73361111,
37.36277778, 47.45, 41.97722222, 33.9425)), .Names = c("zip_long",
"zip_lat", "orig_long", "orig_lat"), row.names = c(NA, -5L), class = c("data.table",
"data.frame")