Turning factor into numeric R

Asked

Viewed 11,076 times

4

Good night!

I am doing a job where I import a base and after the import I create a range of values.

Ex. I import the base with the read.csv

dados <- read.csv(base, header=TRUE, sep=',')

Put in this column of values ($$), which I will use to create the track, when importing changes to factor.
Therefore I use the following code for transformation.

dados$valores <- is.numeric(dados$valores)

What happens here is that the column values change all.

Ex in column value 100, it turns 2600. someone could help me?

3 answers

4


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!

1

Ronaldo, it’s all right?

Adjusting the stringsAsFactors parameter = FALSE prevents Character fields from being converted to factor.

Example:

df <- read.csv("dados.csv", stringsAsFactors = FALSE)
  • 1

    Antonio Thanks a lot for the help, I redid the code and it worked!

-2

dados <- read.csv(base, stringsAsFactors = FALSE)

Modify the function read.csv2

dados <- read.csv2(base, stringsAsFactors = FALSE)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.