Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you really want the levels of the factor to be used, you're either doing something very wrong or too clever for its own good.</p> <p>If what you have is a factor containing numbers stored in the levels of the factor, then you want to coerce it to numeric first using <code>as.numeric(as.character(...))</code>:</p> <pre><code>dat &lt;- data.frame(f=as.character(runif(10))) </code></pre> <p>You can see the difference between accessing the factor indices and assigning the factor contents here:</p> <pre><code>&gt; as.numeric(dat$f) [1] 9 7 2 1 4 6 5 3 10 8 &gt; as.numeric(as.character(dat$f)) [1] 0.6369432 0.4455214 0.1204000 0.0336245 0.2731787 0.4219241 0.2910194 [8] 0.1868443 0.9443593 0.5784658 </code></pre> <p>Timings vs. an alternative approach which only does the conversion on the levels shows it's faster if levels are not unique to each element:</p> <pre><code>dat &lt;- data.frame( f = sample(as.character(runif(10)),10^4,replace=TRUE) ) library(microbenchmark) microbenchmark( as.numeric(as.character(dat$f)), as.numeric( levels(dat$f) )[dat$f] , as.numeric( levels(dat$f)[dat$f] ), times=50 ) expr min lq median uq max 1 as.numeric(as.character(dat$f)) 7835865 7869228 7919699 7998399 9576694 2 as.numeric(levels(dat$f))[dat$f] 237814 242947 255778 270321 371263 3 as.numeric(levels(dat$f)[dat$f]) 7817045 7905156 7964610 8121583 9297819 </code></pre> <p>Therefore, if <code>length(levels(dat$f)) &lt; length(dat$f)</code>, use <code>as.numeric(levels(dat$f))[dat$f]</code> for a substantial speed gain.</p> <p>If <code>length(levels(dat$f))</code> is approximately equal to <code>length(dat$f)</code>, there is no speed gain:</p> <pre><code>dat &lt;- data.frame( f = as.character(runif(10^4) ) ) library(microbenchmark) microbenchmark( as.numeric(as.character(dat$f)), as.numeric( levels(dat$f) )[dat$f] , as.numeric( levels(dat$f)[dat$f] ), times=50 ) expr min lq median uq max 1 as.numeric(as.character(dat$f)) 7986423 8036895 8101480 8202850 12522842 2 as.numeric(levels(dat$f))[dat$f] 7815335 7866661 7949640 8102764 15809456 3 as.numeric(levels(dat$f)[dat$f]) 7989845 8040316 8122012 8330312 10420161 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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