3

I have this data frame individual_dets which has some lat and long values in it. This is the dataframe

individual_dets = structure(list(location = c("ARB-04", "BIRCHY HEAD", "Boca1", "BON-AR-S2", "BON-AR-S2", "BON-W-S5"), month = structure(c(12L, 10L, 10L, 8L, 11L, 2L), .Label = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", "factor")), year = c(2018, 2018, 2018, 2018, 2018, 2018), detection_count = c(3L, 256L, 2L, 4L, 2L, 2L), num_unique_tags = c(1L, 1L, 1L, 1L, 1L, 1L), total_res_time_in_seconds = c(0, 1182040, 0, 2732221, 0, 0), latitude = c(24.94808, 44.5713, 26.32559, -49.27732, -49.27732, -49.27985), longitude = c(-80.45412, -64.03512, -80.07108, 69.48038, 69.48038, 69.47853)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L), groups = structure(list( location = c("ARB-04", "BIRCHY HEAD", "Boca1", "BON-AR-S2", "BON-AR-S2", "BON-W-S5"), month = structure(c(12L, 10L, 10L, 8L, 11L, 2L), .Label = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), class = c("ordered", "factor")), .rows = list(1L, 2L, 3L, 4L, 5L, 6L)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE)) 

However, my dataframe has 4247 observations.

The lat and long values are for coordinates across the ocean in the NW Atlantic. I am trying to calculate the distance from Halifax of these lat and long values.

I have been told to use this function snap points to line and then use lappy but I am looking at the structure and I am so lost. Does anyone understand how to code using the function snap points to line and lappy to get approximate distance?

2
  • What package is snap points to line in? This is not a valid function name in r since it contains spaces. I did find snapPointsToLines in maptools but it specifically warns "This function does not work with geographic coordinates." Commented Feb 9, 2020 at 21:47
  • Ok that was the one I was suggested... I didn't notice. That's probably why it wasn't working for me. Do you know of any other functions I could use? Commented Feb 9, 2020 at 21:49

1 Answer 1

4

You can use st_distance() from the sf package:

library(sf) library(dplyr) individual_dets_sf <- st_as_sf(individual_dets, coords = c("longitude", "latitude"), crs = 4326) %>% ungroup() halifax <- data.frame("longitude" = -63.5752, "latitude" = 44.6488) halifax_sf <- st_as_sf(halifax, coords = c("longitude", "latitude"), crs = 4326, remove = FALSE) individual_dets_sf_2 <- individual_dets_sf %>% mutate(distances = st_distance(., halifax_sf, by_element = TRUE)) individual_dets_sf_2$distances Units: [m] [1] 2664402.84 37510.33 2513963.08 16466708.65 16466708.65 16466623.17 

If you want to visualize it (Halifax in blue):

library(tmap) tmap_mode("view") tm_shape(individual_dets_sf_2 %>% mutate(distances = units::drop_units(distances))) + tm_dots(col = "distances") + tm_shape(halifax_sf) + tm_dots(col = "blue") 

enter image description here

Sign up to request clarification or add additional context in comments.

9 Comments

in the st_as_sf function for the coords = c() do I keep the "longitude", "latitude ? right now I'm getting the error message Error in st_as_sf.data.frame(individual_dets, coords = c("longitude", : missing values in coordinates not allowed but I have way too many observations to put them in individually.
Yes, you need those. They provide the geographic information (i.e., the coordinates) necessary to calculate distances to other places. If you have rows that are missing longitude and/or latitude data, you'll need to remove those points before using st_as_sf().
Glad it worked! Please accept the answer to close the question.
Sorry I'm actually getting all 0m? in the distance column
Ah, I miscopied something (the crs = 4326) was missing. Can you try that first code block again?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.