Convert string containing roman numerals to numeric using R

58 Views Asked by At

I want to convert "stage i", "stage ii", etc to numeric "1" and "2".

pheno_df$pathologic_stage <- gsub("stage ","",pheno_df$pathologic_stage)
as.numeric(factor(pheno_df$pathologic_stage))

Current output:

3 2 3 3 2 5

Desired output:

2 1 2 2 1 4

Data sample:

> dput(pheno_df$pathologic_stage)
c("stage ii", "stage i", "stage ii", "stage ii", "stage i", "stage iv",
1

There are 1 best solutions below

0
Maël On

Extract the numeral part, then convert to roman and back to numeric:

v <- c("stage ii", "stage i", "stage ii", "stage ii", "stage i", "stage iv")
as.numeric(as.roman(gsub("stage ", "", v)))
#[1] 2 1 2 2 1 4