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:
zip | CSZip |
---|---|
91723 | 90048 |
90814 | 90048 |
91604 | 90048 |
90805 | 90048 |
90255 | 90048 |
CodePudding user response:
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
CodePudding user response:
Maybe this is what you need:
dist <- mapply(function(x,y) zip_distance(x,y, lonlat = TRUE, units = "meters"), vf$zip, vf$CSZip)