Mariusz,
Since your vector contains non-numeric values to begin with, it must be a
character or factor vector. What you see as the number 2 is actually the
character string "2", which is, in terms of data type, not numeric.
The code below works in S-plus, and takes advantage of the fact that
as.numeric() returns "NA" for non-numeric strings. It also issues a
warning, which I've suppressed with the options() calls. Note that, after
the substitution, you've still got a character vector. If you want a
numeric vector, you can use as.numeric again, as I've done here:
> values <- c("1","2","3","a","5","6","b","8")
> options(warn=-1); values[is.na(as.numeric(values))] <- 0; options(warn=0)
> values
[1] "1" "2" "3" "0" "5" "6" "0" "8"
> as.numeric(values)
[1] 1 2 3 0 5 6 0 8
>
Hope this helps,
Alan
Alan Hochberg
VP, Research
ProSanos Corporation
225 Market St. Ste. 502,
Harrisburg, PA 17101
Tel 717-635-2124 * Fax 717-635-2575
|