Conversion of data factor to Numeric

Asked

Viewed 3,912 times

3

In the following example:

> str(rhm)
'data.frame':   24 obs. of  4 variables:
 $ plant     : Factor w/ 1 level "LaR": 1 1 1 1 1 1 1 1 1 1 ...
 $ time      : int  0 0 0 0 3 3 3 3 7 7 ...
 $ Tratamento: Factor w/ 4 levels "T1pH1","T1pH2",..: 1 2 3 4 1 2 3 4 1 2 ...
 $ wt        : Factor w/ 20 levels "0,0013","0,0017",..: 20 20 20 20 10 16 18 17 12 19 ...
> str(unclass(rhm$wt))
 atomic [1:24] 20 20 20 20 10 16 18 17 12 19 ...
 - attr(*, "levels")= chr [1:20] "0,0013" "0,0017" "0,0036" "0,0045" ...

When trying to turn the wt variable into number, instead of "0.0013", "0.0017"... appear:

as.numeric(rhm$wt)
[1:24] 20 20 20 20 10 16 18 17 12 19 ...

How to convert wt to numbers?

I tried two suggested ways in forum:

as.numeric(as.character(rhm$wt))
as.numeric(levels(rhm$wt))[rhm$wt]

However occurs the replacement of my data by Nas:

[1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
Warning message:
NAs introduced by coercion 

How can I transform my data correctly?

1 answer

4

In the R the decimal separator is .. For your code to work you have two options:

Use the following code to convert factor in numerical:

as.numeric(gsub(",", ".", as.character(rhm$wt)))

The function gsub with these arguments exchange all commas per point. Then R can convert from chr for numeric. Look at this simple example:

> as.numeric("0,1")
[1] NA
Warning message:
NAs introduced by coercion 
> as.numeric(gsub(",", ".", "0,1"))
[1] 0.1

Another way is: in reading the data, you are probably using the read.table. Use the argument sep = "," indicating that its decimal separator is ,.

  • 1

    Just remembering that option 2, to correct in reading the data, is much more interesting because it solves the problem for all columns at once (if it is the case) and uses fewer characters.

  • 1

    Thank you! Solved!

Browser other questions tagged

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