I have a dataframe which contains a number of columns containing lat long coordinate pairs. I wish to plot these in a map using the leaflet package to see how my data looks.
> str(siteCoor)
'data.frame': 3122 obs. of 18 variables:
$ Longitude: num -8.61 -8.61 -8.61 -8.61 -8.61 ...
$ Latitude : num 52.7 52.7 52.7 52.7 52.7 ...
$ 1 : num -8.6 -8.6 -8.6 -8.58 -8.58 ...
$ 2 : num 52.7 52.7 52.7 52.7 52.7 ...
$ 3 : num -8.61 -8.61 -8.61 -8.58 -8.58 ...
$ 4 : num 52.7 52.7 52.7 52.7 52.7 ...
$ 5 : num -8.62 -8.62 -8.62 -8.58 -8.58 ...
$ 6 : num 52.7 52.7 52.7 52.7 52.7 ...
$ 7 : num -8.62 -8.62 -8.62 -8.59 -8.59 ...
$ 8 : num 52.7 52.7 52.7 52.7 52.7 ...
$ 9 : num -8.63 -8.63 -8.63 -8.59 -8.59 ...
$ 10 : num 52.7 52.7 52.7 52.7 52.7 ...
$ 11 : num -8.63 -8.63 -8.63 -8.6 -8.6 ...
$ 12 : num 52.7 52.7 52.7 52.7 52.7 ...
$ 13 : num -8.64 -8.64 -8.64 -8.6 -8.6 ...
$ 14 : num 52.7 52.7 52.7 52.7 52.7 ...
$ Longitude: num -8.61 -8.61 -8.61 -8.61 -8.61 ...
$ Latitude : num 52.7 52.7 52.7 52.7 52.7 ...
I wish to plot 'Longitude' & 'Latitude', 1&2, 3&4, 5&6, 7&8, 9&10, 11&12, 13&14 and 'Longitude' & 'Latitude' row by row for the entire list. It needs to follow this sequence in order for the polygon to make sense.
Having looked at the help for addPolygons function in R I see that both lat and long need to be in vector format.
Below is the code I am using but when I converted the dataframe to vector format (using double square brackets) I have an error I cannot understand. I also tried using the as.vector function but I could not get it to work.
Could someone please provide a bit of direction?
map <- leaflet() %>%
addTiles() %>%
addPolygons(lng=siteCoor[[,c(1,3,5,7,9,11,13,15,17)]],lat=siteCoor[[,c(2,4,6,8,10,12,14,16,18)]])
The error I am receiving when I run this is:
Error in .subset2(x, ..2, exact = exact) :
recursive indexing failed at level 2
My data looks as follows:
> head(siteCoor[,c(1,3,5,7,9,11,13,15,17)])
Longitude 1 3 5 7 9 11 13 Longitude.1
1 -8.609117 -8.602693 -8.609117 -8.615541 -8.621770 -8.627613 -8.632894 -8.637452 -8.609117
2 -8.609117 -8.602693 -8.609117 -8.615541 -8.621770 -8.627613 -8.632894 -8.637452 -8.609117
3 -8.609117 -8.602693 -8.609117 -8.615541 -8.621770 -8.627613 -8.632894 -8.637452 -8.609117
4 -8.609117 -8.578536 -8.580931 -8.584184 -8.588194 -8.592841 -8.597983 -8.603464 -8.609117
5 -8.609117 -8.578536 -8.580931 -8.584184 -8.588194 -8.592841 -8.597983 -8.603464 -8.609117
6 -8.609117 -8.578536 -8.580931 -8.584184 -8.588194 -8.592841 -8.597983 -8.603464 -8.609117
> head(siteCoor[,c(2,4,6,8,10,12,14,16,18)])
Latitude 2 4 6 8 10 12 14 Latitude.1
1 52.69373 52.71586 52.71620 52.71586 52.71484 52.71319 52.71094 52.70817 52.69373
2 52.69373 52.71586 52.71620 52.71586 52.71484 52.71319 52.71094 52.70817 52.69373
3 52.69373 52.71586 52.71620 52.71586 52.71484 52.71319 52.71094 52.70817 52.69373
4 52.69373 52.70049 52.70361 52.70644 52.70888 52.71085 52.71231 52.71320 52.69373
5 52.69373 52.70049 52.70361 52.70644 52.70888 52.71085 52.71231 52.71320 52.69373
6 52.69373 52.70049 52.70361 52.70644 52.70888 52.71085 52.71231 52.71320 52.69373
addPolygons(lng=siteCoor[,1],lat=siteCoor[,2]) %>% addPolygons(lng=siteCoor[,3],lat=siteCoor[,4])...? - HubertL