Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>See the Warning section of <a href="http://www.inside-r.org/r-doc/base/factor" rel="noreferrer"><code>?factor</code></a>:</p> <blockquote> <p>In particular, <code>as.numeric</code> applied to a factor is meaningless, and may happen by implicit coercion. To transform a factor <code>f</code> to approximately its original numeric values, <code>as.numeric(levels(f))[f]</code> is recommended and slightly more efficient than <code>as.numeric(as.character(f))</code>.</p> </blockquote> <p>The FAQ on R <a href="http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-do-I-convert-factors-to-numeric_003f" rel="noreferrer">has similar advice</a>.</p> <hr> <p><strong>Why is <code>as.numeric(levels(f))[f]</code> more efficent than <code>as.numeric(as.character(f))</code>?</strong></p> <p><code>as.numeric(as.character(f))</code> is effectively <code>as.numeric(levels(f)[f])</code>, so you are performing the conversion to numeric on <code>length(x)</code> values, rather than on <code>nlevels(x)</code> values. The speed difference will be most apparent for long vectors with few levels. If the values are mostly unique, there won't be much difference in speed. However you do the conversion, this operation is unlikely to be the bottleneck in your code, so don't worry too much about it.</p> <hr> <p><strong>Some timings</strong></p> <pre><code>library(microbenchmark) microbenchmark( as.numeric(levels(f))[f], as.numeric(levels(f)[f]), as.numeric(as.character(f)), paste0(x), paste(x), times = 1e5 ) ## Unit: microseconds ## expr min lq mean median uq max neval ## as.numeric(levels(f))[f] 3.982 5.120 6.088624 5.405 5.974 1981.418 1e+05 ## as.numeric(levels(f)[f]) 5.973 7.111 8.352032 7.396 8.250 4256.380 1e+05 ## as.numeric(as.character(f)) 6.827 8.249 9.628264 8.534 9.671 1983.694 1e+05 ## paste0(x) 7.964 9.387 11.026351 9.956 10.810 2911.257 1e+05 ## paste(x) 7.965 9.387 11.127308 9.956 11.093 2419.458 1e+05 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload