s-news
[Top] [All Lists]

Re: Data transformation

To: "'Mariusz'" <setesh@gazeta.pl>, <s-news@lists.biostat.wustl.edu>
Subject: Re: Data transformation
From: "Alan Hochberg" <alan.hochberg@prosanos.com>
Date: Tue, 23 Oct 2007 17:43:45 -0400
In-reply-to: <828851412.20071021144244@gazeta.pl>
Thread-index: AcgVnxBywhurSfMhRsGZrySs5htAcQAHgnuw
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


<Prev in Thread] Current Thread [Next in Thread>