Skip to content
Prev 334229 / 398503 Next

read.csv interpreting numbers as factors

On Tue, Dec 10, 2013 at 10:06 AM, Jeff Newmiller
<jdnewmil at dcn.davis.ca.us> wrote:
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
Message-ID: <CANVKczM584EvF05G8W9f9FOkqmy-+ZRtv-8s47uwXh6Fd_G8Kg@mail.gmail.com>
In-Reply-To: <2a202d445e0c4903b12bf853491b488c@EX-0-HT0.lancs.local>