R Null导致数据帧子集

R Null导致数据帧子集,r,mapping,maptools,R,Mapping,Maptools,在R中,我有一个循环。我想通过循环中的变量来子集数据帧。我希望我的代码如下所示: library(maptools) for(theMonth in 600: 0) { Inspections.mp <- readShapeLines("InspDates2") counties.mp <- readShapePoly("Boundary") plot(counties.mp, axes=FALSE, border="gray") data1 <-Inspect

在R中,我有一个循环。我想通过循环中的变量来子集数据帧。我希望我的代码如下所示:

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)
}
库(maptools)
对于(600:0中的第个月)
{

Inspections.mp这里的问题似乎是一个空子集。这可以通过使用try/catch语句来避免

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")
    })
}
库(maptools)

Inspections.mp您在第一个
数据中缺少了一个尾随逗号1,这要感谢那些循环mod非常好。使用for(60中的第个月)也可以很好地工作。尾随逗号通常是必需的,但由于某些原因会在此处导致错误。似乎将顺序颠倒为for(0:600中的第个月)使其正常工作。我必须找到某种方法使输出图表按相反顺序分配名称。这是什么意思?“很遗憾,这将返回data1中的所有记录,”,另外,在循环中调用
plot
将清除之前绘制的所有内容,因此如果希望所有行都位于同一窗口中,
plot
命令应该位于循环之外。同样正确,Blue Magister。我正在将这些绘图保存到循环之外的png中。我的意思是,“不管是否符合选择条件,所有记录都被放入数据1中。”看起来我的错误在于使用反向计数循环。我不知道这是为什么。
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)
}
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")
    })
}