[R] Problem with script

jim holtman jholtman at gmail.com
Mon Oct 27 01:55:26 CET 2008


It is not a weird error.  It means that one side of the logical test had NA:

> if(1==1) 1
[1] 1
> if (NA==1) 1
Error in if (NA == 1) 1 : missing value where TRUE/FALSE needed
>

So use traceback() to maybe see what is happening.  I would also
suggest that you put

options(error=utils::recover)

in your .Rprofile (or at least type in the command before you run your
script).  This is very handy because on any error it will put you in
the browser where the error occurred which makes debugging easier.  I
leave it on all the time just to handle conditions like this.

On Sun, Oct 26, 2008 at 8:31 PM, Carlos López <natorro at fisica.unam.mx> wrote:
> Hello :-) I am trying to run the next script, it generates "random areas"
> inside a map of the american continent,
> and then plot it, it´s suppose that every frame gives you the evolution of
> the program but at some point it stops
> with the weirdest of the errors I´ve ever seen in R, I don´t even have a
> line like that :-|
>
> Error in if (random_matrix[x_list[random_sq] - 1, y_list[random_sq]] ==  :
>  missing value where TRUE/FALSE needed
>
>
> the script is this:
>
> # ______________
>
>                                       # ----- Define a function for plotting
> a matrix ----- #
> myImagePlot <- function(x, ...){
>  min <- min(x)
>  max <- max(x)
>  yLabels <- rownames(x)
>  xLabels <- colnames(x)
>  title <-c()
>                                       # check for additional function
> arguments
>  if( length(list(...)) ){
>   Lst <- list(...)
>   if( !is.null(Lst$zlim) ){
>     min <- Lst$zlim[1]
>     max <- Lst$zlim[2]
>   }
>   if( !is.null(Lst$yLabels) ){
>     yLabels <- c(Lst$yLabels)
>   }
>   if( !is.null(Lst$xLabels) ){
>     xLabels <- c(Lst$xLabels)
>   }
>   if( !is.null(Lst$title) ){
>     title <- Lst$title
>   }
>  }
>                                       # check for null values
>  if( is.null(xLabels) ){
>   xLabels <- c(1:ncol(x))
>  }
>  if( is.null(yLabels) ){
>   yLabels <- c(1:nrow(x))
>  }
>
>  layout(matrix(data=c(1,2), nrow=1, ncol=2), widths=c(4,1), heights=c(1,1))
>
>                                       # Red and green range from 0 to 1
> while Blue ranges from 1 to 0
>  ColorRamp <- rgb( seq(0,1,length=256),  # Red
>                  seq(0,1,length=256),  # Green
>                  seq(1,0,length=256))  # Blue
>  ColorLevels <- seq(min, max, length=length(ColorRamp))
>
>                                       # Reverse Y axis
>  reverse <- nrow(x) : 1
>  yLabels <- yLabels[reverse]
>  x <- x[reverse,]
>
>                                       # Data Map
>  par(mar = c(3,5,2.5,2))
>  image(1:length(xLabels), 1:length(yLabels), t(x), col=ColorRamp, xlab="",
>       ylab="", axes=FALSE, zlim=c(min,max))
>  if( !is.null(title) ){
>   title(main=title)
>  }
>  axis(BELOW<-1, at=1:length(xLabels), labels=xLabels, cex.axis=0.7)
>  axis(LEFT <-2, at=1:length(yLabels), labels=yLabels, las= HORIZONTAL<-1,
>      cex.axis=0.7)
>
>                                       # Color Scale
>  par(mar = c(3,2.5,2.5,2))
>  image(1, ColorLevels,
>       matrix(data=ColorLevels, ncol=length(ColorLevels),nrow=1),
>       col=ColorRamp,
>       xlab="",ylab="",
>       xaxt="n")
>
>  layout(1)
> }
>                                       # ----- END plot function ----- #
>
>                                       # Read the table to create the matrix
> needed
>
> setwd("/home/natorro/Batmaps/")
> mexico_matrix <- read.table("/home/natorro/Batmaps/mexico_matrix.dat",
> header=TRUE)
>
>                                       # Let's generate a matrix full of
> zeroes
> random_matrix <- matrix(0, nrow=175, ncol=175)
>
>                                       # Size of range (area of distribution)
> range_size <- 100
>
>                                       # Let's choose initial point
>
> initial_point_passed <- FALSE
> initial_point <- sample(175, 2)
> while (initial_point_passed == FALSE){
>  if (mexico_matrix[initial_point[1], initial_point[2]] == 0)
>   initial_point <- sample(175, 2) else {
>     random_matrix[initial_point[1], initial_point[2]] <- 1
>     initial_point_passed <- TRUE
>   }
> }
>
>                                       # Define vectors x_list and y_list
>
> x_list <- matrix(0, nrow=range_size, ncol=1)
> y_list <- matrix(0, nrow=range_size, ncol=1)
>
> x_list[1] <- initial_point[1]
> y_list[1] <- initial_point[2]
>
>
>                                       # Range counter
> range_counter <- 1
> flag <- 0
>
> while (range_counter < range_size){
>  random_sq <- round((runif(1) * range_counter ) + 1)
>  flag <- 0     while (flag == 0) {
>     if(random_matrix[x_list[random_sq]-1, y_list[random_sq]]== 0 ||
>      random_matrix[x_list[random_sq] + 1, y_list[random_sq]] == 0 ||
>      random_matrix[x_list[random_sq], y_list[random_sq] + 1] == 0 ||
>      random_matrix[x_list[random_sq], y_list[random_sq] - 1] == 0) {
>         aleatorio <- runif(1)
>     if (aleatorio < 1) {
>       if (random_matrix[x_list[random_sq],y_list[random_sq]-1]== 0 ) {
>         random_matrix[x_list[random_sq], y_list[random_sq] - 1] <- 1
>         flag <- 1
>         range_counter <- range_counter + 1
>         x_list[range_counter] <- x_list[random_sq]
>         y_list[range_counter] <- y_list[random_sq] - 1
>         myImagePlot(mexico_matrix + random_matrix)
>       }
>     }
>         if (aleatorio < 0.75){
>       if (random_matrix[x_list[random_sq],y_list[random_sq] + 1] == 0) {
>         random_matrix[x_list[random_sq], y_list[random_sq] + 1] <- 1
>         flag <- 1
>         range_counter <- range_counter + 1
>         x_list[range_counter] <- x_list[random_sq]
>         y_list[range_counter] <- y_list[random_sq] + 1
>         myImagePlot(mexico_matrix + random_matrix)
>       }
>     }
>             if (aleatorio < 0.50){
>       if(random_matrix[x_list[random_sq]+1, y_list[random_sq]] == 0) {
>         random_matrix[x_list[random_sq] + 1, y_list[random_sq]] <- 1
>         flag <- 1
>         range_counter <- range_counter + 1
>         x_list[range_counter] <- x_list[random_sq] + 1
>         y_list[range_counter] <- y_list[random_sq]
>         myImagePlot(mexico_matrix + random_matrix)
>       }
>     }
>         if (aleatorio < 0.25) {
>       if(random_matrix[x_list[random_sq]-1, y_list[random_sq]] == 0 ) {
>         random_matrix[x_list[random_sq] - 1, y_list[random_sq]] <- 1
>         flag <- 1
>         range_counter <- range_counter + 1
>         x_list[range_counter] <- x_list[random_sq] - 1
>         y_list[range_counter] <- y_list[random_sq]
>         myImagePlot(mexico_matrix + random_matrix)
>       }
>     }
>           }
>  }
> }
>
> sum(random_matrix)
>
>
> #-------------------------------
>
>
> I do the "sum(random_matrix)" just to check out if the number of zeroes
> needed have been set (never has reached 100 so far :-()
>
> I´m not sure what I am doing wrong, I´ve been working with this file in
> windows and mac at the same time in a colaboration.
> Any help will be greatly appreciated.
>
> Carlos
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list