[R] Convert character Variables to numeric

MacQueen, Don macqueen1 at llnl.gov
Thu Aug 27 02:25:27 CEST 2015


In addition to what Jim and Jeff said, I would suggest trying this:

tmp <- is.na(V)
## then
head(Mac$TOTALFRT[tmp])
## or
head( V[tmp] )

This will show you the first few rows of the values which you think should
be numeric but are not.

Hopefully, you will be able to see why they are not numeric. Perhaps, as
Jim suggested, they contain commas, which are not considered numeric.

I¹d also suggest
  Mac <- read.csv("July'15.csv", header = TRUE, stringsAsFactors=FALSE)
While you are trying to solve this problem. This is because you said
TOTALFRT is showing as character, in which case there should be no need to
use as.character(Mac$TOTALFRT).

-- 
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





On 8/26/15, 1:30 AM, "R-help on behalf of Arun84441"
<r-help-bounces at r-project.org on behalf of arun.kumar8 at safexpress.com>
wrote:

>I have imported the csv file having 398800 obs of 30 variables. There is a
>variable "TOTALFRT" which is showing as character in R environment. I am
>trying to convert it to numeric by using as.numeric function.
>Wherein function is converting character to numeric but with an error
>message "Warning message:
>NAs introduced by coercion", it omitted 388800 entries. Hence I am unable
>to
>perform calculation.
>
>Is there anyway by which I can convert NAs to numeric??
>
>Below is the code I used for conversion :-
>
>Mac<-read.csv("July'15.csv", header = TRUE)
>Book=as.character(Mac$TOTALFRT)
>V = as.numeric(Book)
>
>
>
>
>
>
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Convert-character-Variables-to-numeric-tp471
>1518.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.



More information about the R-help mailing list