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