Dale Steele
2007-Dec-12 21:35 UTC
[R] problem applying a conditional formula to each element of a matrix
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
Warning message:
In if (h <= phi) { :
the condition has length > 1 and only the first element will be used
# function
Cov.f <- function(h, sigmasq, phi) {
if (h <= phi) {Cij <- sigmasq * (1 - ( 1.5 * (h/phi) - 0.5 *
(h/phi)^3)) } else
if (h > phi) {Cij <- 0}
return(Cij)
}
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(coords, method="euclidean"))
distance.matrix
Cov.f(distance.matrix, 3.9, 58.1)
Domenico Vistocco
2007-Dec-12 22:45 UTC
[R] problem applying a conditional formula to each element of a matrix
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 any class.
In your case you have a matrix of logical values:
> distance.matrix <= 58.1
1 2 3
1 TRUE TRUE TRUE
2 TRUE TRUE FALSE
3 TRUE FALSE TRUE
A possible solution (maybe not the better):
Cov.f <- function(h, sigmasq, phi) {
Cij <- h
Cij[h<=phi] <- sigmasq * (1 - ( 1.5 * (Cij[h<=phi]/phi) - 0.5
*(Cij[h<=phi]/phi)^3))
Cij[h>phi] <- 0
return(Cij)
}
domenico
Dale Steele wrote:> 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
>
> Warning message:
> In if (h <= phi) { :
> the condition has length > 1 and only the first element will be used
>
> # function
>
> Cov.f <- function(h, sigmasq, phi) {
> if (h <= phi) {Cij <- sigmasq * (1 - ( 1.5 * (h/phi) - 0.5 *
> (h/phi)^3)) } else
> if (h > phi) {Cij <- 0}
> return(Cij)
> }
>
> 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(coords, method="euclidean"))
> distance.matrix
> Cov.f(distance.matrix, 3.9, 58.1)
>
> ______________________________________________
> 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
2007-Dec-12 22:50 UTC
[R] problem applying a conditional formula to each element of a matrix
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(coords, method="euclidean")) > distance.matrix1 2 3 1 0.00000 32.51538 34.94138 2 32.51538 0.00000 67.27146 3 34.94138 67.27146 0.00000> Cov.f(distance.matrix, 3.9, 58.1)1 2 3 1 3.9000000 0.9678766 0.8059627 2 0.9678766 3.9000000 0.0000000 3 0.8059627 0.0000000 3.9000000>On Dec 12, 2007 1:35 PM, Dale Steele <dale.w.steele at gmail.com> wrote:> 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 > > Warning message: > In if (h <= phi) { : > the condition has length > 1 and only the first element will be used > > # function > > Cov.f <- function(h, sigmasq, phi) { > if (h <= phi) {Cij <- sigmasq * (1 - ( 1.5 * (h/phi) - 0.5 * > (h/phi)^3)) } else > if (h > phi) {Cij <- 0} > return(Cij) > } > > 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(coords, method="euclidean")) > distance.matrix > Cov.f(distance.matrix, 3.9, 58.1) > > ______________________________________________ > 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 you are trying to solve?