I have a dataframe with many (100+) pairs of coordinates
[lat1] [long2] [lat3] [long4] [..] [..]
30.12 70.25 32.21 70.25 .. ..
31.21 71.32 32.32 75.2 .. ..
32.32 70.25 31.23 75.0 .. ..
This function draws a line connecting the coordinates in the first two columns of the dataframe
lines(mapproject(x=data$long2, y=data$lat1), col=3, pch=20, cex=.1)
I need to perform this function on every pair of lat/lon coordinates so that there is a new/unconnected line drawn for each one
Looking at this example Operate on every two columns in a matrix I think I need to create a list, how do I do that for each column pair?
Then, I can use lapply as described here https://nicercode.github.io/guides/repeating-things/ - how should I wrap my lines() call within a function?
Full script:
library(maps)
library(mapproj)
data <- read.csv("data.csv")
map('world', proj='orth', fill=TRUE, col="#f2f2f2", border=0, orient=c(90, 0, 0))
lines(mapproject(x=data$long, y=data$lat), col=3, pch=20, cex=.1)
UPDATED QUESTION
With some help from the comments I've tried a method that performs sapply on every two columns, which seems to work as desired. This is open to improvements
df <- data.frame(X1 = c(0, 10, 20),
Y2 = c(80, 85, 90),
X3 = c(3, 10, 15),
Y4 = c(93, 100, 105),
X5 = c(16, 20, 35),
Y6 = c(100, 105, 130))
map('world', proj='orth', fill=TRUE, col="#f2f2f2", border=0, orient=c(90, 0, 0))
sapply(seq(1,5,by=2),function(i) lines(mapproject(x = (df[,i]), y = (df[,(i+1)])), col = 3))