0

In R, I have a loop. I would like to subset a dataframe by the variable in the loop. I would like my code to look like this:

library(maptools)
for(theMonth in 600: 0)
{
  Inspections.mp <- readShapeLines("InspDates2")
  counties.mp <- readShapePoly("Boundary")
  plot(counties.mp, axes=FALSE, border="gray")
  data1 <-Inspections.mp[Inspections.mp$MonthInt == theMonth]
  lines(data1, col="blue", lwd=1)
}

Unfortunately, this returns all of the records in data1, and using the line

data1 <-Inspections.mp[Inspections.mp$MonthInt == theMonth,]

causes the following error: Error in bb[1, ] : incorrect number of dimensions

I can, however, get the records I want when just using a constant integer, but I need a variable

library(maptools)
for(theMonth in 600: 0)
{
  Inspections.mp <- readShapeLines("InspDates2")
  counties.mp <- readShapePoly("Boundary")
  plot(counties.mp, axes=FALSE, border="gray")
  data1 <-Inspections.mp[Inspections.mp$MonthInt == 60,]
  lines(data1, col="blue", lwd=1)
}
ike
  • 126
  • 8
  • You're missing a trailing comma in your first `data1 <- ...` expression. Also, there is no reason to do your read steps inside the loop since they are constant. You could also add a `print(theMonth)` for debugging. Does your loop work with `for(theMonth in 60)`? – Justin Feb 01 '13 at 19:02
  • Thanks those loop mods are great. Using for(theMonth in 60) also works perfectly. The trailing comma is normally necessary, but for some reason is causing an error here. – ike Feb 01 '13 at 19:10
  • Seems like reversing the order to for(theMonth in 0:600) causes it to work correctly. I'll have to just find some way to get my output charts to assign names in reverse order. – ike Feb 01 '13 at 19:13
  • What does this mean? "Unfortunately, this returns all of the records in data1,", Also, calling `plot` in your loop will wipe out whatever you had plotted before, so if you want all the lines in the same window the `plot` command should be outside the loop. – Blue Magister Feb 01 '13 at 19:13
  • Also correct, Blue Magister. I'm saving those plots to a png outside the loop. I meant to say something like, "All of the records are placed into data1, regardless of whether it meets the selection criteria." It looks like my error is in using a reverse counting loop. I don't know why this is. – ike Feb 01 '13 at 19:16
  • Hm. I'm curious about the `incorrect number of dimensions` error. `class(Inspections.mp)` is a data frame? What does `str(Inspections.mp$MonthInt == theMonth)` look like for `theMonth in 600:590`? – Blue Magister Feb 01 '13 at 19:29
  • Month 600 (and a few others) returns no records. This is probably why the dimension error crops up. How do I null result proof this subselect? – ike Feb 01 '13 at 19:34
  • str(Inspections.mp$MonthInt == theMonth) looks like: logi [1:39330] FALSE FALSE FALSE FALSE FALSE FALSE ... etc for ten lines – ike Feb 01 '13 at 19:43
  • Enclosing in a try catch statement seems to fix this. Thank you very much Justin for your help with my loop overwork, and thank you very much Blue Magister for pointing out my null result problem. I'll post a solution in a second. – ike Feb 01 '13 at 20:04
  • It would be cleaner for others to read if you posted your edit as a solution. – Blue Magister Feb 01 '13 at 20:13
  • I tried, I'm new here, I have to wait 10 hours. If you could post it as a solution, I'll mark it! And I'll edit my question of course. – ike Feb 01 '13 at 20:17

1 Answers1

0

The problem here seems to be a null subset. This is avoided by using a try/catch statement.

library(maptools)

Inspections.mp <- readShapeLines("InspDates2")
counties.mp <- readShapePoly("Boundary")
for(theMonth in 600: 0)
{
  plot(counties.mp, axes=FALSE, border="gray")

  result <- tryCatch({
    data1 <-Inspections.mp[Inspections.mp$MonthInt == theMonth,]
    lines(data1, col="blue", lwd=1)
    },warning = function(war){
      print("WARNING")
    },error = function (err)
    {
      print("Error")
    }, finally = {
      print("Finally")
    })
}
ike
  • 126
  • 8