[R] Convert character Variables to numeric

Jeff Newmiller jdnewmil at dcn.davis.CA.us
Wed Aug 26 22:52:25 CEST 2015


I agree that having a few (5-20 perhaps) rows of the data is essential to give you appropriate help. Be warned that files with extension ".CSV" (or must others as well) don't make it through the mailing list filters, so be sure to read the Posting Guide and either change the name of the file so it ends in ".txt" or use the dput function on data after you have read it in [1] and just include that in your email not as an attachment.

Note that adding certain arguments to the read.csv function call can make it do you don't have to convert from factor to character:

Mac <- read.csv("July'15.csv", header = TRUE, as.is=TRUE)

or

Mac <- read.csv("July'15.csv", header = TRUE, stringsAsFactors=FALSE)

[1] http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
--------------------------------------------------------------------------- 
Sent from my phone. Please excuse my brevity.

On August 26, 2015 1:33:28 PM PDT, jim holtman <jholtman at gmail.com> wrote:
>Please provide a sample of the data that you are trying to convert. 
>BTW
>does it have commas in numeric values, or what else is strange about
>the
>data.  The error message is very clear in the column of data that you
>are
>trying to convert is not numeric.
>
>
>Jim Holtman
>Data Munger Guru
>
>What is the problem that you are trying to solve?
>Tell me what you want to do, not how you want to do it.
>
>On Wed, Aug 26, 2015 at 4:30 AM, Arun84441 <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-tp4711518.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.
>>
>
>	[[alternative HTML version deleted]]
>
>______________________________________________
>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