[R] missing value where TRUE/FALSE needed
jim holtman
jholtman at gmail.com
Fri Dec 23 14:26:22 CET 2011
Does this look similar to the error you are getting:
> while(NA == TRUE) 1
Error in while (NA == TRUE) 1 : missing value where TRUE/FALSE needed
SO 'notconverged' is probably equal to NA. BTW, what is the value of
'tol'; I do not see it defined. So when computing 'notconverged' you
have generated an NA. You can test it to see if this is true.
You can use the following command:
options(error=utils::recover)
and then learn how to use the 'browser' to examine variables when the
error occurs.
On Fri, Dec 23, 2011 at 5:44 AM, Michael Pearmain
<michael.pearmain at gmail.com> wrote:
> Merry Xmas to all,
>
> I am writing a function and curiously this runs sometimes on one data set
> and fails on another and i cannot figure out why.
> Any help much appreciated.
>
> If i run the code below with
> data <- iris[ ,1:4]
> The code runs fine, but if i run on a large dataset i get the following
> error (showing data structures as matrix is large)
>
>> str(cluster.data)
> num [1:9985, 1:811] 0 0 0 0 0 0 0 0 0 0 ...
> - attr(*, "dimnames")=List of 2
> ..$ : NULL
> ..$ : chr [1:811] "1073949105" "1073930585" "1073843224" "1073792624" ...
> #(This is intended to be chr)
>> str(iris)
> 'data.frame': 150 obs. of 5 variables:
> $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
> $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
> $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
> $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
> $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1
> 1 1 1 ...
>> str(as.matrix(iris[,1:4]))
> num [1:150, 1:4] 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
> - attr(*, "dimnames")=List of 2
> ..$ : NULL
> ..$ : chr [1:4] "Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width"
>
> n.cols <- ncol(data)
> n.rows <- nrow(data)
> X <- as.matrix(data)
> stepsize <- 0.05
> c1 <- (2 * pi) ** (n.cols / 2)
> c2 <- n.rows * (smoothing ** (n.cols + 2))
> c3 <- n.rows * (smoothing ** n.cols)
>
> Kexp <- function(sqs){
> return (exp((-1 * sqs) / (2 * smoothing ** 2)))
> }
>
> FindGradient <- function(x){
> XmY <- t(x - t(X))
> sqsum <- rowSums(XmY * XmY)
> K <- sapply(sqsum, Kexp)
> dens <- ((c1 * c3) ** -1) * sum(K)
> grad <- -1 * ((c1 * c2) ** -1) * colSums(K * XmY)
> return (list(gradient = grad,
> density = dens))
> }
>
> attractors <- matrix(0, n.rows, n.cols)
> densities <- matrix(0, n.rows)
>
>
>> density.attractors <-
> sapply(rep(1:n.rows), function(i) {
> notconverged <- TRUE
> # For each row loop through and find the attractor and density value.
> x <- (X[i, ])
> iters <- as.integer(1)
> # Run gradient ascent for each point to obtain x*
> while(notconverged == TRUE) {
> find.gradient <- FindGradient(x)
> next.x <- x + stepsize * find.gradient$gradient
> change <- sqrt(sum((next.x - x) * (next.x - x)))
> notconverged <- ifelse(change > tol, TRUE, FALSE)
> x <- next.x
> iters <- iters + 1
> }
>
> # store the attractor and density value
> return(c(densities[i, ] <- find.gradient$density,
> attractors[i, ] <- x))
> })
>
> Error in while (notconverged == TRUE) { :
> missing value where TRUE/FALSE needed
>>
>
> Any help would be great
>
> Mike
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.
More information about the R-help
mailing list