2

I'm trying to build something like this http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/ but with my own data in csv files. The code runs well if I use the same csv files as the author, but with mine , this is what I get

Code

library(maps) 
library(geosphere) 


map("world") 

xlim <- c(-180.00, 180.00) 

ylim <- c(-90.00, 90.00) 

map("world", col = "#f2f2f2", fill = TRUE, bg = "white", lwd = 0.05,xlim = xlim, ylim = ylim) 

airports <- read.csv("/Users/shabnam/Desktop/airports.csv", as.is=TRUE, header=TRUE) 
flights <- read.csv("/Users/shabnam/Desktop/flights.csv", as.is=TRUE, header=TRUE) 

 pal <- colorRampPalette(c("#545454", "white"))colors <- pal(100) 
map("world", col="#303030", fill=TRUE, bg="black", lwd=0.05, xlim=xlim, ylim=ylim) 


fsub <- flights[flights$airline == "AA",] 


fsub <- fsub[order(fsub$cnt),] 

maxcnt <- max(fsub$cnt) 

for (j in 1:length(fsub$airline)) 
{ 
air1 <- airports[airports$iata == fsub[j,]$airport1,] 

air2 <- airports[airports$iata == fsub[j,]$airport2,] 

inter <- gcIntermediate(c(air1[1,]$long, air1[1,]$lat), c(air2[1,]$long, air2[1,]$lat), n=100, addStartEnd=TRUE) 

colindex <- round( (fsub[j,]$cnt / maxcnt) * length(colors) ) 

lines(inter, col=colors[colindex], lwd=0.8) 
} 


Error in if (antipodal(p1, p2)) { : missing value where TRUE/FALSE needed 

I've tried everything, adding breakAtDateLine, sp and sepNA but to no use.

Would really appreciate any help, this is the first time I'm experimenting with R.

Roland
  • 127,288
  • 10
  • 191
  • 288
  • Can you provide a minimal reproducing set of example data, so we can experiment with this? What does `traceback()` tell you about the exact location where this error occurred? – MvG Nov 25 '12 at 07:24
  • Sure here's http://pastebin.com/f2pPtt5B (flightssh.csv) and http://pastebin.com/P2iW3hGd (airportssh.csv) I've used countries instead of cities/airports but that shouldn't be an issue right ? – Shabnam Shaik Nov 25 '12 at 08:39
  • Error in if (antipodal(p1, p2)) { : missing value where TRUE/FALSE needed traceback() 2: .interm(p[i, 1:2, drop = FALSE], p[i, 3:4, drop = FALSE], p[i, 5]) 1: gcIntermediate(c(air1[1, ]$long, air1[1, ]$lat), c(air2[1, ]$long, air2[1, ]$lat), n = 100, addStartEnd = TRUE) at #5 – Shabnam Shaik Nov 25 '12 at 08:40
  • Often this is due to a logical comparison returning `NA`, which can happen if `NA` is an input. So, find out what `antipodal` is returning, and then find out what the values (and types) `p1` and `p2` are. – Carl Witthoft Nov 25 '12 at 13:53
  • In the sample data provided, @CarlWitthoft is exactly right. when j=1, air1 and air2 are zero length because there are no matches, so when you pass them to gcIntermediate(), it throws an error. It is plausible that the problem w your full data set is related. – MattBagg Nov 25 '12 at 14:35
  • Thanks @MattBagg and carl -my tables looked like this: row.names iata airport lat long 1 21 BE Belgium 50.8333 4 2 NA NA NA NA NA I tried removing rows containing NA using this for (j in 2:length(fsub$airline)) { air1 <- airports[airports$iata == fsub[j,]$airport1,] row.has.na <- apply(air1, 1, function(x){any(is.na(x))}) sum(row.has.na) air1.filtered <- air1[!row.has.na,] air2 <- airports[airports$iata == fsub[j,]$airport2,] row.has.na <- apply(air2, 1, function(x){any(is.na(x))}) sum(row.has.na) air2.filtered <- air2[!row.has.na,] Still d same. How can I improve d for loop ? – Shabnam Shaik Nov 25 '12 at 17:51
  • If you edit the question and add the code, you can select the code format option and it will be much more readable. In the reduced set, the problem was not that air1 or air2 contained NA. The problem seemed to be that they are length 0 when there are no connections between the two airports. – MattBagg Nov 25 '12 at 19:17

0 Answers0