title: R: need finite 'ylim' values in function
body: <p>I'd like to plot the data in data.frame <code>xy</code> for each group (defined by <code>ID</code>). When a year before 1946 is in a group, <code>plot 2</code> should be executed. When the years are between 1946 and 2014, <code>plot1</code> should be executed. </p>
  
  <p>My problem: This works fine without NA values, but as I have data gaps I rely on NAs  to define these data gaps. This is why I get an error: <code>error in plot.window(need finite 'ylim' values)</code>. I tried to put <code>finite=T</code> in <code>plot1</code> at the y-axis but this gives a <code>subscript out of bounds</code> error. Is there a way I could solve this and that the graphics are correctly plotted?</p>
  
  <p>In the following is my code: It is long but most of the code consists of <code>plot()</code> options on which I rely.</p>
  
  <pre><code># read in sample data and split it up by group (defined by ID)
  xy &lt;- data.frame(NAME=c("NAME2","NAME2","NAME2","NAME2","NAME2","NAME3","NAME3","NAME3","NAME3","NAME5","NAME5","NAME5","NAME5"), ID=c(48,48,48,48,48,32,32,32,32,67,67,67,67),YEAR=c(1981,1983,1984,1988,1989,1984,1984,1988,1988,1899,1933,1948,1958),VALUE=c(0,205,-570,0,-310,-3680,-3680,NA,-3680,0,NA,13,-98))
  ind &lt;- split(x = xy,f = xy[,'ID'])
  
  # Plot Scenario 1: if only years between 1946 and 2014 are present for each group do this:
    plot1 &lt;- function(x) {
    fname &lt;- paste0(x[1, 'ID'], '.png')
    png(fname, width=1679, height=1165, res=150)
    par(mar=c(6,8,6,5))
    plot(x = c(1946, 2014),
         y = range(x$VALUE),
         type='n',
         main=x[1, 'NAME'],
         xlab="Time [Years]",
         ylab="Value")
    axis(2, at = seq(-100000, 100000, 500), cex.axis=1, labels=FALSE, tcl=-0.3)
    points(ind[[i]][,c('YEAR','VALUE')], type="l", lwd=2)
    points(ind[[i]][,c('YEAR','VALUE')], type="p", lwd=1, cex=1,   pch=21, bg='white')
    abline(h=0)
    dev.off()
  }
  
  # Plot Scenario 2 if years under 1946 are present do this:
  plot2 &lt;- function(x) {
    fname &lt;- paste0(x[1, 'ID'], '.png')
    png(fname, width=1679, height=1165, res=150)    
    par(mar=c(6,8,6,5))
    plot(x[,c('YEAR','VALUE')],
         type='n',
         main=x[1, 'NAME'],
    xlab="Time [Years]",
    ylab="Value [mm]")
  axis(2, at = seq(-100000, 100000, 500), cex.axis=1, labels=FALSE, tcl=-0.3)
  points(ind[[i]][,c('YEAR','VALUE')], type="l", lwd=2)
  points(ind[[i]][,c('YEAR','VALUE')], type="p", lwd=1, cex=1,   pch=21, bg='white')
  abline(h=0)
  dev.off() 
  }
  
  # Execute functions
      lapply(ind, function(x) ifelse(any(x$YEAR &lt; 1946 &amp; x$YEAR &lt; 2014), plot2(x), plot1(x)))
  </code></pre>