Re: [R] problem applying a conditional formula to each element of a matrix

2007-12-12 Thread jim holtman
I think you want to use 'ifelse': > Cov.f <- function(h, sigmasq, phi) { + ifelse(h <= phi, sigmasq * (1 - ( 1.5 * (h/phi) - 0.5 * (h/phi)^3)), 0) + } > > x.coord <- c(5.7, 6.7, 9.8) > y.coord <- c(42.7, 10.2, 77.4) > coords <- cbind(x.coord, y.coord) > distance.matrix <- as.matrix(dist(coord

Re: [R] problem applying a conditional formula to each element of a matrix

2007-12-12 Thread Domenico Vistocco
The conditional have to be a single element: > ?"if" cond: A length-one logical vector that is not 'NA'. Conditions of length greater than one are accepted with a warning, but only the first element is used. Other types are coerced to logical if possible, ignoring

[R] problem applying a conditional formula to each element of a matrix

2007-12-12 Thread Dale Steele
I'm applying a function (Cov.f) defined below to each element of a distance matrix. When I run the code below, I get a warning message (below) and elements of returned matrix [2,3] and [3,2] are not zero as I would expect. Clearly, there is an error... What am I doing wrong? Thanks. --Dale Warni