将zip_distance应用于每一行

发布于 2025-01-25 07:46:47 字数 790 浏览 0 评论 0原文

我有一个带有两列的数据框,zipcszip。我正在尝试使用以下方式将函数应用于每一行:

dist <- apply(vf, 1, zip_distance(zip, CSZip, lonlat = TRUE, units = "meters"))

但是我得到此错误:

Error in as.character(zipcode_a) : 
  cannot coerce type 'closure' to vector of type 'character'

zip_distance来自zipcoder软件包。

这就是5行VF的样子:

ZipCSZIP
9172390048
9081490048
9160490048
9004890805 90048
90048 9025590048

I have a dataframe with two columns, zip and CSZip. I am trying to apply a function to each row using:

dist <- apply(vf, 1, zip_distance(zip, CSZip, lonlat = TRUE, units = "meters"))

But I get this error:

Error in as.character(zipcode_a) : 
  cannot coerce type 'closure' to vector of type 'character'

zip_distance is from the ZipCodeR package.

This is what 5 rows of vf looks like:

zipCSZip
9172390048
9081490048
9160490048
9080590048
9025590048

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幽蝶幻影 2025-02-01 07:46:47

Zipcoder具有一些时髦的行为。
有时,使用循环更容易:

library(zipcodeR)

vf <- read.table(header=TRUE, text="zip CSZip
91723   90048
90814   90048
91604   90048
90805   90048
90255   90048")


for(i in 1:nrow(vf)) {
   vf$dist[i] <- zip_distance(vf$zip[i], vf$CSZip[i], lonlat = TRUE, units = "meters")$distance
}
vf

The ZipcodeR is has some funky behavior.
Sometimes it is just easier to use a for loop:

library(zipcodeR)

vf <- read.table(header=TRUE, text="zip CSZip
91723   90048
90814   90048
91604   90048
90805   90048
90255   90048")


for(i in 1:nrow(vf)) {
   vf$dist[i] <- zip_distance(vf$zip[i], vf$CSZip[i], lonlat = TRUE, units = "meters")$distance
}
vf

也许这就是您需要的:

dist <- mapply(function(x,y) zip_distance(x,y, lonlat = TRUE, units = "meters"), vf$zip, vf$CSZip)

Maybe this is what you need:

dist <- mapply(function(x,y) zip_distance(x,y, lonlat = TRUE, units = "meters"), vf$zip, vf$CSZip)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文