# [R] converting numerical parts of factors into numeric vectors

Jan.Schelling@hydro.com Jan.Schelling at hydro.com
Fri Apr 28 14:45:43 CEST 2000

```
Try:

as.numeric(levels(x)[x])

However, this gives you a warning if you have any non-numerical levels as
mentioned in the help file for 'as.numeric'.

Jan

-------------------------
Jan Schelling
Norsk Hydro
Research Centre
N - 3907 Porsgrunn
NORWAY

From: wolters at ikp.uni-bonn.de on 2000-04-28 10:23 GMT

To:   r-help at stat.math.ethz.ch
cc:    (bcc: Jan Schelling/HRE/Hydro)

Subject:  [R] converting numerical parts of factors into numeric vectors

Dear list,

Problem:
Given a factor that contains both numerical values and category codes,
how can I extract the numerical values and store them in a numeric
vector while preserving the exact values? as.numeric() replaces the
numerical values by their factor levels. But since the values represent
an interval-scaled distance measure, this is not exactly what is called
for.

Current workaround: replace category codes by arbitrary numbers to be filtered
out later. In this case, negative numbers.

More elegant solution: ?

I checked CRAN, V&R, and the online help, but to no avail.

see "Current Workaround" ;)

Maria Wolters
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```