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

Arthur Steinmetz apsteinmetz at yahoo.com
Thu Jan 31 21:57:30 CET 2008


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

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.

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]))}

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?



More information about the R-help mailing list