I'm trying to use the "apply" function with the R "eleavatr" package. I'm getting an error message but I'm not sure if the problem is in my understanding of the apply function (perhaps the root of the problem?) or a wholly different problem. The function works if you just use it alone ie get_elev(temp_df) but using it in apply causes problem. This is my sample data & code:
> temp_df
x y
41 -74.67020 41.36181
42 -74.66993 41.36208
43 -74.66966 41.36235
44 -74.66939 41.36262
45 -74.66912 41.36289
46 -74.66885 41.36316
47 -74.66858 41.36343
48 -74.66831 41.36370
49 -74.66804 41.36397
50 -74.66777 41.36424
get_elev<-function(ll){
temp_el<-elevatr::get_elev_point(locations = ll, src = "epqs", units="meters",
prj = ll_prj)
return(temp_el)
}
sapply(temp_df, FUN=get_elev) #this should take the lat/lon coordinates of each row to search for elevation.
Error message:
Error in if (attributes(class(locations)) %in% c("raster", "sp")) { :
argument is of length zero
sapply(temp_df, print)
to see what each iteration is feeding to the function.... If you want to do things row-wise, useapply(temp_df, 1, FUN=something)
.[1] -74.67020 -74.66993 -74.66966 -74.66939 -74.66912 -74.66885 -74.66858 -74.66831 -74.66804 -74.66777 [1] 41.36181 41.36208 41.36235 41.36262 41.36289 41.36316 41.36343 41.36370 41.36397 41.36424 x y [1,] -74.67020 41.36181 [2,] -74.66993 41.36208
. (sorry I don't know how to format this better). Is there a way to feed both values into apply?get_elev_point
with the whole data frame?