Bill
2013-Oct-27 21:33 UTC
[R] numeric data being interpreted as a factor -trouble with reading data into a dataframe in R
Hello. trying to do one of the simplest actions -read in data into R. I don't know why the FBfollowers column is being read as a factor and also if I use as.numeric on it, it looks really strange and actually complety alters the data. I am attaching the data set here called ddd.csv I used data=read.csv("ddd.csv",header=TRUE) fb=data$FBfollowers fb fb=as.numeric(fb) fb Thnxs in advance
Jeff Newmiller
2013-Oct-28 01:23 UTC
[R] numeric data being interpreted as a factor -trouble with reading data into a dataframe in R
No data came through (I don't think "CSV" is one of the approved file types.) You probably have some odd characters somewhere in the data. Try adding the argument stringsAsFactors=FALSE to the read.csv call before converting the troublesome column.. Converting factors to numeric converts the integer representation, not the character representation. Once you identify which rows are corrupt, you can look at them more closely. You may be able to formulate a regex pattern that removes the invalid characters with the sub function before conversion. --------------------------------------------------------------------------- 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. Bill <william108 at gmail.com> wrote:>Hello. >trying to do one of the simplest actions -read in data into R. >I don't know why the FBfollowers column is being read as a factor and >also >if I use as.numeric on it, it looks really strange and actually >complety >alters the data. >I am attaching the data set here called ddd.csv >I used >data=read.csv("ddd.csv",header=TRUE) > >fb=data$FBfollowers >fb >fb=as.numeric(fb) >fb > >Thnxs in advance > > >------------------------------------------------------------------------ > >______________________________________________ >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.
PIKAL Petr
2013-Oct-31 09:28 UTC
[R] numeric data being interpreted as a factor -trouble with reading data into a dataframe in R
Hi Reading numeric as factor can have many causes from weird formating to some nonumeric characters. If you can not clean it when making *.csv file you shall either adopt reading function by using different options see ?read.table or if it does not help, you can either polish your values by some regular expressions. For simple changing factor values to numeric you shall use fb <- as.numeric(as.character(fb)) But all values which are not transferable to numeric will be changed to NA Regards Petr> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Bill > Sent: Sunday, October 27, 2013 10:33 PM > To: r-help at r-project.org > Subject: [R] numeric data being interpreted as a factor -trouble with > reading data into a dataframe in R > > Hello. > trying to do one of the simplest actions -read in data into R. > I don't know why the FBfollowers column is being read as a factor and > also if I use as.numeric on it, it looks really strange and actually > complety alters the data. > I am attaching the data set here called ddd.csv I used > data=read.csv("ddd.csv",header=TRUE) > > fb=data$FBfollowers > fb > fb=as.numeric(fb) > fb > > Thnxs in advance