0

First of all, I'm new at R, I'm just learning.

I have a data frame and I want to make some plots and graphics with two variables, one of these variables is read as a factor but this variable is with real numbers. This variable is a percentage so I want to graphics this percentage related to some municipalities, how can I transform these numbers to numeric values?

I've tried this following code because in the guide I'm reading its say to convert factors to numeric with the function as.numeric() but the result is totally different numbers.

for example

  #the data frame is valle.abu2
  valle.abu2$Porcentaje.de.Excedencias

  #then 
   as.numeric(valle.abu2$Porcentaje.de.Excedencias)




   valle.abu2$Porcentaje.de.Excedencias
    [1] 1.3   0.04  1.6   0     0     0     0.31  0.61  0     2.31  3.6   8.04  0     7.18  0     5.88  1.35  0    
   [19] 2.56  0     3.2   0     0     0     0     0     0.05  0.32  0     5.23  0     0     0     0     0     0    
   [37] 0     5.42  5.54  11.44 0     2.51  0     4.88  0     3.45  0     2.78  2.7   0     4.39  0     0     0    
   [55] 0     3.99  3.42  6.01  0     5.52  0     0.04  0     0.46  0.34  0     4.63  0     14.65 2.91  5.9   4.17 
   [73] 0     0     0     0     0     0     1.15  1.52  9.17  2.22  3.82  0     0     0     0     7.04  3.57  12.5 
   [91] 0     0     0     0.72  1.32  0     9.88  2.63  0     0     0     0     0     0     37.57
   134 Levels: 0 0.03 0.04 0.05 0.06 0.07 0.09 0.1 0.11 0.14 0.15 0.23 0.27 0.29 0.31 0.32 0.33 0.34 0.42 

 as.numeric(valle.abu2$Porcentaje.de.Excedencias)

   [1]  42   3  48   1   1   1  15  25   1  69  92 129   1 127   1 120  44   1  71   1  86   1   1   1   1   1   4
   [28]  16   1 115   1   1   1   1   1   1   1 116 118  59   1  70   1 108   1  90   1  75  73   1 103   1   1   1
  [55]   1  97  89 122   1 117   1   3   1  21  18   1 104   1  64  77 121 101   1   1   1   1   1   1  39  47 131
  [82]  67  96   1   1   1   1 126  91  60   1   1   1  28  43   1 134  72   1   1   1   1   1   1  98
Tamara Koliada
  • 1,200
  • 2
  • 14
  • 31
  • 1
    To convert `factor` to `numeric`, you have to convert to `character` first, otherwise you get the numeric factor levels. `as.numeric(as.character(valle.abu2$Porcentaje.de.Excedencias))` will do it. – Mako212 Feb 15 '19 at 16:46

1 Answers1

0

Try:

as_numeric_factor <- function(x){
  as.numeric(levels(x))[x]
}

as_numeric_factor(valle.abu2$Porcentaje.de.Excedencias)

Explanation.

The help page ?factor section Warning includes two different ways of doing what the question asks for, and states that one of them is more efficient.

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)).

Here is a simple speed test. set.seed is not needed since the result of interest are the timings, not computations.

library(microbenchmark)
library(ggplot2)

as_numeric_factor2 <- function(x){
  as.numeric(as.character(x))
}


f <- factor(rnorm(1e4))
mb <- microbenchmark(
  levl = as_numeric_factor(f),
  char = as_numeric_factor2(f)
)

autoplot(mb)

enter image description here

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Diego Rojas
  • 199
  • 6
  • but then I try to see the class and it is still a factor, and I need to do some operations with this numbers so the problems still the same. – Alejandro Jaramillo Feb 15 '19 at 17:06
  • @OttoRoquetto Did you actually save the result some place? If you want to replace the original column, you can do `valle.abu2$Porcentaje.de.Excedencias <- as_numeric_factor(valle.abu2$Porcentaje.de.Excedencias)` – MrFlick Feb 15 '19 at 17:19