0

I'm using R for the first time for a study on grasslands and I need to clip my findings onto a specific map. The trouble I'm having is the coordinates in the data don't match the coordinates on the map, they're mirror images. It's probably extremely simple since I can't find any tutorials or code online for it. Or I'm bad at wording this in search bars, either way, does anyone know how to do this transformation?

  • could you add some details about the data you are trying to use and may be a screenshot of what's going wrong – Ian Turton Oct 17 '16 at 16:35
1

I agree with @iant, more detail is needed, but I'm guessing your coordinates are either in two columns dataframe or a list of tuples. Assuming you have a dataframe df with columns lat and lon as the first two columns, you might switch the order of the columns:

df <- data.frame(lat=c(1,3), lon=c(2,4), more_cols = ...)
df
#  lat lon ...
#1   1   2 ...
#2   3   4 ...
df <- df[,c('lon', 'lat', other_col_names)] 
#or df <- df[c('lon', 'lat', other_col_names)
df
#  lon lat ...
#1   2   1 ...
#2   4   3 ...

Alternately, if you have a list coords of coordinate vectors or tuples, you might switch the order of each coordinate pair with the following code:

coords <- list(c(1,2), c(3,4))
for (coord_index in 1:length(coords)){
    coords[[coord_index]] <- coords[[coord_index]][c(2,1)]
}
coords
#[[1]]
#[1] 2 1
#
#[[2]]
#[1] 3 4

Hope this helps.

  • Why would you swap columns? All you have to do is define the correct columns when coercing into a sp object. The columns are not retained and are stored in the coordinates slot. FYI, you do not need a for loop for coercing to a matrix or data.frame object. Just use: do.call("rbind", coords) – Jeffrey Evans Oct 17 '16 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.