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

From: Gavin Simpson <>
Date: Fri, 01 Feb 2008 00:23:46 +0000

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
> 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


> 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.

 Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e]
 Gower Street, London          [w]
 UK. WC1E 6BT.                 [w]

______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 01 Feb 2008 - 00:27:54 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 01 Feb 2008 - 00:30:10 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive