Hi all,
I've got a question regarding kriging outputs. I have an interpolated
dataset which due to the nugget effect contains some negative values as
the predictions. I would like to truncate these @ "0", rather than
having them as a negative prediction.
I've tried something similar with the meuse data set...
data(meuse.grid)
coordinates(meuse.grid) = c("x", "y")
gridded(meuse.grid) <- TRUE
meuse.grid[["idist"]] = 1 - meuse.grid[["dist"]]
Somewhat arbitrary, but essentially, I'd like to set all "dist" data less than 0.5 to "0", and have the rest remain as they are...
meuse.grid[["class"]] = for(i in 1:length(meuse.grid[["dist"]])){
if (meuse.grid[["dist"]]<0.5) {meuse.grid[["class"]]==0} else
{meuse.grid[["class"]]=meuse.grid[["dist"]]}
}
All I've managed to get is the new attribute "class" all filled with 0.
Any insight would be appreciated.
kriging output
5 messages · Dave Depew, Edzer Pebesma, Roger Bivand
Dave Depew wrote:
Hi all,
I've got a question regarding kriging outputs. I have an interpolated
dataset which due to the nugget effect contains some negative values
as the predictions. I would like to truncate these @ "0", rather than
having them as a negative prediction.
I've tried something similar with the meuse data set...
data(meuse.grid) coordinates(meuse.grid) = c("x", "y")
gridded(meuse.grid) <- TRUE
meuse.grid[["idist"]] = 1 - meuse.grid[["dist"]]
Somewhat arbitrary, but essentially, I'd like to set all "dist" data
less than 0.5 to "0", and have the rest remain as they are...
meuse.grid[["class"]] = for(i in 1:length(meuse.grid[["dist"]])){
if (meuse.grid[["dist"]]<0.5) {meuse.grid[["class"]]==0} else
I guess you meant an = rather than a == here
{meuse.grid[["class"]]=meuse.grid[["dist"]]}
}
All I've managed to get is the new attribute "class" all filled with 0.
Try: meuse.grid$class = meuse.grid$dist meuse.grid$class[meuse.grid$dist < .5] = 0 plot(class~dist,meuse.grid) -- Edzer
Thanks,
This worked.
I'm still confused why the if else statement didn't work...
If one wanted to do conditional arithmetic would a for statement bee needed?
e.g.
meuse.grid[["class"]] = for(i in 1:length(meuse.grid[["dist"]])){
if (meuse.grid[["dist"]]<0.5)
{meuse.grid[["class"]]=10*meuse.grid[["dist]]} else
{meuse.grid[["class"]]=100* meuse.grid[["dist"]]}
}
Edzer Pebesma wrote:
Dave Depew wrote:
Hi all,
I've got a question regarding kriging outputs. I have an interpolated
dataset which due to the nugget effect contains some negative values
as the predictions. I would like to truncate these @ "0", rather than
having them as a negative prediction.
I've tried something similar with the meuse data set...
data(meuse.grid) coordinates(meuse.grid) = c("x", "y")
gridded(meuse.grid) <- TRUE
meuse.grid[["idist"]] = 1 - meuse.grid[["dist"]]
Somewhat arbitrary, but essentially, I'd like to set all "dist" data
less than 0.5 to "0", and have the rest remain as they are...
meuse.grid[["class"]] = for(i in 1:length(meuse.grid[["dist"]])){
if (meuse.grid[["dist"]]<0.5) {meuse.grid[["class"]]==0} else
I guess you meant an = rather than a == here
{meuse.grid[["class"]]=meuse.grid[["dist"]]}
}
All I've managed to get is the new attribute "class" all filled with 0.
Try: meuse.grid$class = meuse.grid$dist meuse.grid$class[meuse.grid$dist < .5] = 0 plot(class~dist,meuse.grid) -- Edzer
Dave Depew wrote:
Thanks,
This worked.
I'm still confused why the if else statement didn't work...
If one wanted to do conditional arithmetic would a for statement bee
needed?
e.g.
meuse.grid[["class"]] = for(i in 1:length(meuse.grid[["dist"]])){
if (meuse.grid[["dist"]]<0.5)
{meuse.grid[["class"]]=10*meuse.grid[["dist]]} else
{meuse.grid[["class"]]=100* meuse.grid[["dist"]]}
}
I'd say meuse.grid$class = ifelse(meuse.grid$dist < .5, 10, 100) * meuse.grid$dist assigning the result from a for statement also does not yield a beauty price if you ask me, but that might be my backgrounds in C, long ago. -- Edzer
On Mon, 5 May 2008, Edzer Pebesma wrote:
Dave Depew wrote:
Thanks,
This worked.
I'm still confused why the if else statement didn't work...
If one wanted to do conditional arithmetic would a for statement bee
needed?
e.g.
meuse.grid[["class"]] = for(i in 1:length(meuse.grid[["dist"]])){
if (meuse.grid[["dist"]]<0.5)
{meuse.grid[["class"]]=10*meuse.grid[["dist]]} else
{meuse.grid[["class"]]=100* meuse.grid[["dist"]]}
}
I'd say meuse.grid$class = ifelse(meuse.grid$dist < .5, 10, 100) * meuse.grid$dist
Right. ifelse() is vectorised, but if() else is not, so the vector condition in ifelse() splits out nicely, but if() else is flow control on a scalar condition. The above for loop looks redundant anyway (no use of index), but the if() else construction wouldn't work either - as you found out earlier. For things like this, Braun & Murdoch is good (and from Canada!); if you read German, Uwe Ligges' book is recommended. It's also in the online Introduction to R, section 9.2.1, which explains the previous paragraph much more clearly than I can. Roger
assigning the result from a for statement also does not yield a beauty price if you ask me, but that might be my backgrounds in C, long ago. -- Edzer
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no