-1

If numeric data convert to factor with manipulated levels or labels like :

original <- c(1.1,2.1,2.1,1.1,2.1)
manipulated <- factor(original, labels = c("one", "two") )

Is there any way to convert manipulated to original ? if not, is not better it was reversable?

Iman
  • 2,224
  • 15
  • 35
  • I think, the question was discussed here already: https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information – Wolfgang Arnold Mar 31 '20 at 11:03

1 Answers1

0

No, there's no way to reverse that change. If you did

original <- c(1.1,2.1,2.1,1.1,2.1)
manipulated <- factor(original)

then the values 1.1 and 2.1 would be converted to character strings and stored as the levels. But your conversion

manipulated <- factor(original, labels = c("one", "two") )

overwrites the levels with c("one", "two"), so the original values are no longer part of manipulated.

If you wanted the change to be reversible, you could do the manipulation in two steps:

original <- c(1.1,2.1,2.1,1.1,2.1)
manipulated <- factor(original)
savedLevels <- levels(manipulated)
levels(manipulated) <- c("one", "two")

restored <- as.numeric( savedLevels[manipulated] )
restored
#> [1] 1.1 2.1 2.1 1.1 2.1

It's not guaranteed to be perfect since character versions of numbers aren't exact, but it's going to be very close.

user2554330
  • 37,248
  • 4
  • 43
  • 90