[R] read.csv interpreting numbers as factors

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Tue Dec 10 11:48:11 CET 2013


On Tue, Dec 10, 2013 at 10:06 AM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
> It is bad netiquette to hijack an existing thread for a new topic. Please start a new email thread when changing topics.
>
> If your data really consists of what you show, then read.csv won't behave that way. I suggest that you open the file in a text editor and look for odd characters. They may be invisible.
>
> Going out on a limb, you may be trying to read a tab separated file, and if so then you need to use the sep=”\t" argument to read.csv.

Or something in the data isn't a valid number. Try:

as.numeric(as.character(factorthingyouthinkshouldbenumbers))

and if you get any NA values then those things aren't valid number
formats. You need as.numeric(as.character(..)) because otherwise
as.numeric just gets the underlying number codes for the factor
levels.


 > f=factor(c("1","1","2","three","4","69"))
 > f
[1] 1     1     2     three 4     69
Levels: 1 2 4 69 three

 > as.numeric(f)
[1] 1 1 2 5 3 4

 > as.numeric(as.character(f))
[1]  1  1  2 NA  4 69
Warning message:
NAs introduced by coercion



More information about the R-help mailing list