[R] numeric coercion when one or more elements is non numerice

Gavin Simpson gavin.simpson at ucl.ac.uk
Fri Feb 1 01:23:46 CET 2008


hits=-2.6 tests=BAYES_00
X-USF-Spam-Flag: NO

On Thu, 2008-01-31 at 12:57 -0800, Arthur Steinmetz wrote:
> That did it. Thanks!
> 
> What I was getting was
> 
> > temp
>           GDP  CPIYOY
> 23832  3108.2 garbage
> 23923 garbage     1.8
> 24015  3214.1     1.8
> 24107  3291.8       2
> > str(temp)
> `data.frame':   4 obs. of  2 variables:
>  $ GDP   :Error in importIntoEnv(impenv, impnames, ns, impvars) : 
>         objects 'dev.interactive', 'palette', 'extendrange', 'xy.coords' are not exported by 'namespace:grDevices'
> > 

That looks very, very strange to me. How did you get temp into your
workspace? From a text file? You shouldn't get errors when you do
str(object) if things are working as intended. I'd try to get to the
bottom of that before proceeding as you never know what problems that
might cause in future computations.

> so your tip of 
> 
> > 
> temp$GDP 
> <- 
> as.numeric(as.character(temp$GDP))
> 
> did the trick.
> 
> 
> Two twists: 
> 1. I am not sure what characters will actually be garbage but I see
> that as.numeric coerces any garbage to be NA automatically so I don't
> search and replace.

If you can, try not to rely upon side effects and the like. The side
effect here is that as.numeric will try to coerce to numeric for things
that are reasonable but obviously produces NA when trying to convert
"garbage" to a numeric variable. I come from a field where -99.99 was
used as an NA, that will get converted to numeric using the approach I
mentioned and that is not what you want.

> 
> 2. Since I contemplated 'garbage' scattered about I need to step
> through the vectors in the frame
> 
> for (j in 1:dim(hist)[2]){hist[,j]<-
> as.numeric(as.character(hist[,j]))}

If you need to work over the rows/columns of a data frame ?apply is your
friend. If you just need to work on the columns of your data frame look
at ?sapply and ?lapply

G

> 
> Although I suspect, given how powerful R is, there is a way to avoid
> the for loop and operate on the whole frame.  Anyway, problem solved!
> 
> 
> 
> 
>       ____________________________________________________________________________________
> Looking for last minute shopping deals?  
> Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list