This problem is quite frequent when working with class objects data.frame
, which is the class of objects that functions read.table
and derivatives produce.
To avoid this, just see that default the value of the argument stringsAsFactors
is TRUE
. Behold help("read.csv")
, for example.
Note that the read.csv
only reads numbers as factor
if those numbers have non-numeric characters. There must be something in the file that forces R to interpret the values as strings. (alphanumeric?)
The solution to not create a class column factor
should be
dados <- read.csv(base, stringsAsFactors = FALSE)
(I omitted the other arguments because they already have these values. In fact, read.csv
is just the function read.table
with header = TRUE
, sep = ","
and others.)
There are still two other ways to transform class objects factor
in numeric
.
dados$valores <- as.numeric(as.character(dados$valores))
dados$valores <- as.numeric(levels(dados$valores))[dados$valores]
From the help page help("factor")
, section Warning
:
To Transform a factor f to approximately its original Numeric values,
as.Numeric(levels(f))[f] is Recommended and Slightly more Efficient
than as.Numeric(as.Character(f)).
Update - R 4.0.0 (2020-04-24)
With the new R 4.0.0 (2020-04-24)
the conduct of the duties read.*
changed.
Of R News, section CHANGES IN R 4.0.0.
R now uses a stringsAsFactors = FALSE
default, and Hence by default
no longer converts strings to factors in calls to data.frame()
and
read.table()
.
A large number of Packages relied on the Previous behaviour and so
have needed/will need updating.
Translation:
O R uses now stringsAsFactors = FALSE
, and therefore by default
no longer converts strings into factors in calls to data.frame()
and
read.table()
.
A large number of packages were based on the previous behavior and, by
this, they needed/will have to be updated.
Rui, Thanks for the help and the explanation, super enlightened, I did not know that read.csv read the data as factor, once again I thank you for the help!
– Ronaldo Ribeiro