0

I have the following structure to my dataset

     dms_long              dms_lat              
[1,] "1   3° 41' 19.9\" W" "1  40° 25' 35.8\" N"
[2,] "2   3° 47' 42.2\" W" "2  40° 26' 28.4\" N"
[3,] "3   3° 41' 11.7\" W" "3  40° 28' 39.8\" N"
[4,] "4                 "  "4                 " 
[5,] "5   3° 55' 29.6\" W" "5  38° 59' 15.0\" N"

I am trying to convert it so that it follows the following structure;

         dms_long         dms_lat
1: 3° 41' 19.9" W 40° 25' 35.8" N
2: 3° 47' 42.2" W 40° 26' 28.4" N
3: 3° 41' 11.7" W 40° 28' 39.8" N
4:                               
5: 3° 55' 29.6" W 38° 59' 15.0" N

I am trying to remove the \ and the first and last " which should give me 38° 59' 15.0" N but cannot seem to get this format using gsub. If anybody has any idea I would appreciate it.

EDIT: Cannot seem to get the following code to run correctly which is why I thought I had errors in formatting.

pts <- cbind(dms_long, dms_lat)
pts <- sub("^\\d+\\s+", "", pts)
pts[pts==""] <- NA
pts <- pts[complete.cases(pts),]
pts <- matrix(as.numeric(sp::char2dms(as.vector(pts), "°")), ncol=2)
guscht
  • 843
  • 4
  • 20
user113156
  • 6,761
  • 5
  • 35
  • 81
  • 2
    The `\` isn't really there.... It's just an escape. – A5C1D2H2I1M1N2O1R2T1 Dec 21 '17 at 14:21
  • 1
    There is no backslash in your strings. R just displays a backslash before the quote so that you know that that is not the quote that surrounds the string but rather is part of the string. – G. Grothendieck Dec 21 '17 at 14:23
  • Thanks! I did not know! I do continue to run into errors when I run additional code which is why I though the formatting was off. (I have edited the original post to show the code) – user113156 Dec 21 '17 at 14:25
  • 1
    What is your error message? – guscht Dec 21 '17 at 15:24
  • #1 you learnt about dput in [your last question](https://stackoverflow.com/questions/47914862/plotting-in-rworldmaps), why don't you continue to use it? #2 Why do you open another question instead of first commenting on [the answers](https://stackoverflow.com/questions/47914862/plotting-in-rworldmaps#47916168) to the old one, which addresses the same problem? Just curious about the logic... – lukeA Dec 21 '17 at 15:31
  • Sure, I will continue to use dput from here on out. Regarding the "why I opened a new question". I have responded asking "different" questions previously to the original questions I have asked and the usual response I get is "this is a different question to the one originally asked, please open a new question" so thats my logic behind opening a new question. I have still open the question you are refering to and have not forgot about it and I will thank LukeA as soon as I get the whole data file working as his solution looks very good. – user113156 Dec 21 '17 at 15:39
  • You _Cannot seem to get the following code to run correctly_? Doesn't it yield a `structure(c(-3.68886111111111, -3.79505555555556, -3.68658333333333, -3.92488888888889, 40.4266111111111, 40.4412222222222, 40.4777222222222, 38.9875), .Dim = c(4L, 2L))`? – Armali Feb 05 '20 at 09:24

0 Answers0