[R] numeric data being interpreted as a factor -trouble with reading data into a dataframe in R

PIKAL Petr petr.pikal at precheza.cz
Thu Oct 31 10:28:29 CET 2013


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



More information about the R-help mailing list