Hutchinson,David [PYR]
2008-Sep-16 22:19 UTC
[R] Unexpected returned value from a function
Hi R-Users,
I wrote a simple function to change values of a matrix or vector to NA
based on the element value being -9999 or -999999. I don't understand
why the function returns a unit vector (NA) instead of setting all
values in the vector which have -9999 or -999999 to NA. When I apply the
function in line, it appears to work correctly?
Can someone enlighten me what I am doing wrong?
Thanks in advance.
David
Here's my example:
ConvertMissingToNA <- function (values) {
return ( values[ values == -9999 | values == -999999] <- NA )
}
d <- floor(runif(10, 1, 100))
pos <- floor (runif(5, 1, 10))
d[pos] <- -9999
pos <- floor (runif(2, 1, 10))
d[pos] <- -999999
print (d)
# now apply function
e <- ConvertMissingToNA (d) # will return NA as a unit vector
print (e)
# conduct function in-line
d[ d == -9999 | d == -999999] <- NA # correctly converts values to NA
print (d)
try this -- you have to return the entire vector:
ConvertMissingToNA <- function (values) {
values[ values == -9999 | values == -999999] <- NA
values
}
d <- floor(runif(10, 1, 100))
pos <- floor (runif(5, 1, 10))
d[pos] <- -9999
pos <- floor (runif(2, 1, 10))
d[pos] <- -999999
print (d)
# now apply function
e <- ConvertMissingToNA (d) # will return NA as a unit vector
print (e)
# conduct function in-line
d[ d == -9999 | d == -999999] <- NA # correctly converts values to NA
print (d)
On Tue, Sep 16, 2008 at 6:19 PM, Hutchinson,David [PYR]
<David.Hutchinson at ec.gc.ca> wrote:> Hi R-Users,
>
> I wrote a simple function to change values of a matrix or vector to NA
> based on the element value being -9999 or -999999. I don't understand
> why the function returns a unit vector (NA) instead of setting all
> values in the vector which have -9999 or -999999 to NA. When I apply the
> function in line, it appears to work correctly?
>
> Can someone enlighten me what I am doing wrong?
>
> Thanks in advance.
>
> David
>
>
> Here's my example:
>
>
> ConvertMissingToNA <- function (values) {
> return ( values[ values == -9999 | values == -999999] <- NA )
> }
>
> d <- floor(runif(10, 1, 100))
> pos <- floor (runif(5, 1, 10))
> d[pos] <- -9999
> pos <- floor (runif(2, 1, 10))
> d[pos] <- -999999
> print (d)
>
> # now apply function
> e <- ConvertMissingToNA (d) # will return NA as a unit vector
> print (e)
>
> # conduct function in-line
> d[ d == -9999 | d == -999999] <- NA # correctly converts values to NA
> print (d)
>
> ______________________________________________
> 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?
Quoting "Hutchinson,David [PYR]" <David.Hutchinson at ec.gc.ca>:> I wrote a simple function to change values of a matrix or vector to NA > based on the element value being -9999 or -999999. I don't understand > why the function returns a unit vector (NA) instead of setting all > values in the vector which have -9999 or -999999 to NA. When I apply the > function in line, it appears to work correctly? > > Can someone enlighten me what I am doing wrong?> return ( values[ values == -9999 | values == -999999] <- NA )the assignment itself evaluates to the assigned value, in this case NA; that's what your function returns. Check this:> foo <- (a <- 3) > a[1] 3> foo[1] 3> foo <- (a <- NA) > a[1] NA> foo[1] NA this way your function should work as intended: ConvertMissingToNA <- function(values) { values[ values == -9999 | values == -999999] <- NA return values } Peter
What you want is
ConvertMissingToNA <- function (values) {
values[ values == -9999 | values == -999999] <- NA
return( values )
}
To see why your version doesn't do what you wanted, maybe it helps to
consider the following?
x <- 1:10
y <- (x[3:6] <- 99)
y ## 99
(It's perhaps not entirely obvious that the value of y should be 99
and not c(99,99,99,99), but anyway, neither correspond to what you
wanted.)
Dan
On Tue, Sep 16, 2008 at 03:19:53PM -0700, Hutchinson,David [PYR]
wrote:> Hi R-Users,
>
> I wrote a simple function to change values of a matrix or vector to NA
> based on the element value being -9999 or -999999. I don't understand
> why the function returns a unit vector (NA) instead of setting all
> values in the vector which have -9999 or -999999 to NA. When I apply the
> function in line, it appears to work correctly?
>
> Can someone enlighten me what I am doing wrong?
>
> Thanks in advance.
>
> David
>
>
> Here's my example:
>
>
> ConvertMissingToNA <- function (values) {
> return ( values[ values == -9999 | values == -999999] <- NA )
> }
>
> d <- floor(runif(10, 1, 100))
> pos <- floor (runif(5, 1, 10))
> d[pos] <- -9999
> pos <- floor (runif(2, 1, 10))
> d[pos] <- -999999
> print (d)
>
> # now apply function
> e <- ConvertMissingToNA (d) # will return NA as a unit vector
> print (e)
>
> # conduct function in-line
> d[ d == -9999 | d == -999999] <- NA # correctly converts values to NA
> print (d)
>
> ______________________________________________
> 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.
--
http://www.stats.ox.ac.uk/~davison