Convert roman numerals to numbers in R

后端 未结 3 2065
遥遥无期
遥遥无期 2020-12-03 17:22

In R, there is a great function as.roman in the very base setup:

as.roman(79)
# [1] LXXIX

Is there an inverse function that wo

3条回答
  •  隐瞒了意图╮
    2020-12-03 17:28

    as.roman() returns an object of class roman, so R recognizes it as such. You can directly turn it back into an Arabic numeral with as.numeric(). If you have a string that meets the criteria such that it could be a valid roman numeral, you can coerce it to a class roman object with as.roman(), and then coerce it into an Arabic numeral by composing the coercion functions. Consider:

    > as.roman(79)
    [1] LXXIX
    > x <- as.roman(79)
    > x
    [1] LXXIX
    > str(x)
    Class 'roman'  int 79
    > as.roman("LXXIX")
    [1] LXXIX
    > as.numeric(as.roman("LXXIX"))
    [1] 79
    

提交回复
热议问题