0

I have a data set that looks like this

        lat_deg lat_min long_deg long_min
site 1     44     4.971    80     27.934
site 2
site 3
site 4
site 5


site <- c(1,2,3)
lat_deg <- c(44,44,44)
lat_min <- c(4.971, 4.977, 4.986)
long_deg <- c(80,80,80)
long_min <- c(27.934, 27.977, 27.986)
df <- data.frame(site, lat_deg, lat_min, long_deg, long_min)

How do I convert this into degree decimal only? All my lats are in N and longs in W, so I'm not too worried except the final sign should be correct. Additionally, will I be able to calculate altitude from here?

Note: All other questions on SO focus on DMS to DD. This question has not been asked before.

Rspacer
  • 2,369
  • 1
  • 14
  • 40
  • 1
    Perhaps I'm totally wrong, but isn't it simply dividing the minutes by 60? So 44° 4.971' would be 44.08285 ? – Martin Gal Jul 03 '21 at 23:22
  • You should use `dput()` to share your data on stackoverflow. Your table is not a valid R data frame since "site 1" is not a valid row number. The equation you need is degrees + minutes/60 + seconds/3600 or just degrees + minutes/60 for your example. – dcarlson Jul 03 '21 at 23:23
  • @MartinGal I am not sure then how to "join" the numerical columns? – Rspacer Jul 03 '21 at 23:33
  • Just add them together? – Martin Gal Jul 03 '21 at 23:35

1 Answers1

2

Based on the data given, here is a approach using dplyr:

df %>%
  mutate(lat = lat_deg + lat_min/60,
         long = long_deg + long_min/60)

returns

  site lat_deg lat_min long_deg long_min      lat     long
1    1      44   4.971       80   27.934 44.08285 80.46557
2    2      44   4.977       80   27.977 44.08295 80.46628
3    3      44   4.986       80   27.986 44.08310 80.46643

or simply

df$lat <- df$lat_deg + df$lat_min/60
df$long <- df$long_deg + df$long_min/60
Martin Gal
  • 16,640
  • 5
  • 21
  • 39
  • 1
    You may also do this within `across` (based on your original approach with `across`) i.e. `df %>% mutate(across(ends_with('min'), ~ .x/60 + get(str_replace(cur_column(), "min", "deg")), .names = "{str_remove(.col, '_.*')}"))` – akrun Jul 03 '21 at 23:40
  • Please post this as answer. Very elegant way! – Martin Gal Jul 03 '21 at 23:42
  • 1
    But, that is similar to your initial approach and would n't be that fast as your updated post which is enough for two column cases – akrun Jul 03 '21 at 23:42