0

I am using a model to create prediction. The model is giving me a factor out which ranges from 0 to 6.

I am trying to report this as this value, but when I try to convert this to a number or put it into a data frame, it converts the 0 value to a 1 and all the other values up one...sometimes.

out = as.factor(c(0,1,2,3,4,5))
out
[1] 0 1 2 3 4 5
Levels: 0 1 2 3 4 5

as.numeric(out)
[1] 1 2 3 4 5 6

I would simply subtract by 1 if this increased the value by 1 everytime, but if my model returns only non-zero values, it will not increase the value:

out = as.factor(c(1,2,3,4,5,6))
as.numeric(out)
[1] 1 2 3 4 5 6

Is there a simple way to get the raw values out of the factor rather than R converting the 0 to a 1 and adjusting the rest of the values?

Thank you,

RStudio 1.3.1093 r 4.0.3

Steve
  • 31
  • 3
  • `as.numeric(as.character(out))`. Without the `as.char` part, it's returning the number of the integer indices; see `as.numeric(factor(c('a','1')))` versus `as.numeric(as.character(factor(c('a','1'))))` (which should emit a warning). – r2evans Apr 19 '21 at 18:54
  • You should note from this that *"get the raw values ... and adjusting the rest of the values"* is a risky proposition: if everything is 0-based and everything is in the right order and nothing it *gapped*, then this might be safe ... if any of those conditions is not met, however, then you will silently corrupt your data. – r2evans Apr 19 '21 at 18:55
  • 1
    Thank you for the comments. I found a solution here: https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information – Steve Apr 19 '21 at 19:05

1 Answers1

1

From my own comments, I found the solution here: How to convert a factor to integer\numeric without loss of information?

"In particular, as.numeric applied to a factor is meaningless, and may happen by implicit coercion. 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))." - Joshua Ulrich

This solved the issue and I was able to put into a data frame without a problem.

Steve
  • 31
  • 3