仅用R中的罗马数字替换数据框列的编号

时间:2018-01-22 19:58:59

标签: r replace

您好我已将此示例创建为我的数据框的可重现列。

const playersByGender = {
  mens: [],
  womens: [1, 2],
  other: []
};

const onlyOne = (obj) => Object.keys(obj).reduce((sum, k) => obj[k].length > 0 ? sum + 1 : sum, 0) === 1;

console.log(onlyOne({
  mens: [],
  womens: [1, 2],
  other: []
}));

console.log(onlyOne({
  mens: [],
  womens: [1, 2],
  other: [1]
}));

我想要做的是用相对罗马数字替换所有数字,用sa<-c("Phase 1","Phase 2","Phase 1 | Phase 2","Phase 4") 替换|而不更改单词&#34;阶段&#34;。

我尝试将数字与:

隔离开来
\

但是我不能正确地替换它们

还有其他建议吗?

4 个答案:

答案 0 :(得分:3)

首先捕获比赛

m <- gregexpr("(\\d)", sa)

现在使用sapply申请as.roman

regmatches(sa, m) <- sapply(regmatches(sa, m), as.roman)

sa现在是:

sa
[1] "Phase I"            "Phase II"           "Phase I | Phase II"
[4] "Phase IV" 

最后使用gsub

gsub(" \\| ", " / ", sa)

获得所需的输出

[1] "Phase I"            "Phase II"           "Phase I / Phase II"
[4] "Phase IV"

答案 1 :(得分:2)

试一试。

library(gsubfn)

new.sa <- gsubfn(pattern = '(\\d)',
                 replacement = as.list(setNames(as.character(as.roman(as.character(1:100))),as.character(1:100))),
                  x = sa)
new.sa
#[1] "Phase I"            "Phase II"           "Phase I | Phase II" "Phase IV"   
newer.sa <- gsub('[|]', '/', new.sa)
newer.sa
#[1] "Phase I"            "Phase II"           "Phase I / Phase II" "Phase IV" 

编辑:在从本杰明那里了解as.roman()之后修改了我的答案。

答案 2 :(得分:0)

这并不是特别优雅,但只要你不处理数百万条目,它就应该运作得很好。它使用as.roman包中的gtools函数。

library(gtools)
library(stringr)

sa<-c("Phase 1","Phase 2","Phase 1 | Phase 2","Phase 4")


sub_roman <- function(x){
  # identify any numbers (up to three digits)
  num <- as.numeric(unlist(str_extract_all(x, "\\d{1,3}")))
  for (i in seq_along(num)){
    # loop through the numbers and replace with the roman numeral
    x <- str_replace(x, "\\d{1,3}", as.character(as.roman(num[i])))
  }
  x
}

# Run the previously defined function over the vector.
sa <- 
  vapply(sa,
         sub_roman,
         character(1))

# replace the pipe with a slash.
sa <- str_replace_all(sa, "[|]", "/")

答案 3 :(得分:0)

您还可以使用哈希表来映射键值对并进行字符串替换。

library(stringr)
library(data.table)
library(hashmap)

sa <- c("Phase 1","Phase 2","Phase 1 | Phase 2","Phase 4")

temp <- data.table(stringr::str_replace_all(sa, "[|]", "/"))

temp <- temp %>%
  mutate(K1 = as.roman(1 : 4)) 

H <- hashmap(as.character(temp$K1), temp$V1)

H2 <- hashmap(H$keys(), H$values())

sa <- data.table(stringr::str_replace_all(H2$values(), "\\d", H2$keys()))