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

From: Arthur Steinmetz <apsteinmetz_at_yahoo.com>
Date: Thu, 31 Jan 2008 12:57:30 -0800 (PST)


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?

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 31 Jan 2008 - 21:10:38 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 - 01:30:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive