R:在数据框中突出显示同一句子中的多个项目

时间:2019-06-25 09:06:31

标签: r replace highlight gsub

我有一个表格,其中包含我要突出显示的单词和我要突出显示的文本的列表:

df <- data_frame(
  tags = list(list("sphinx", "judge", "vow"), list("jackdaws", "sphinx", "love"), list()),
  text = list("Sphinx of black quartz, judge my vow", "Jackdaws love my big sphinx of quartz", 
           "Carved symbols in a mountain hollow on the bank of an inlet irritated an eccentric person.")
) %>%
  mutate(text = as.character(text))

我正在考虑做

gsub(df$tags[1][[1]][1], paste0('<span style="background-color: #FFFF00">', df$tags[1][[1]][1], '</span>'), df$text[1], ignore.case = TRUE)

但是,我一直在努力寻找如何替换同一句子中的多个内容的方法,例如,如果执行循环操作,它将覆盖自身,并且只会替换列表中的最后一个单词。

此外,我也不完全确定如何使其在数据框架内工作。

预期的输出将类似于

df_output <- data_frame(
      tags = list(list("sphinx", "judge", "vow"), list("jackdaws", "sphinx", "love"), list()),
      text = list("<span style="background-color: '#FFFF00'">Sphinx</span> of black quartz, <span style="background-color: '#FFFF00'">judge</span> my <span style="background-color: '#FFFF00'">vow<span>", "<span style="background-color: '#FFFF00'">Jackdaws</span> <span style="background-color: '#FFFF00'">love</span> my big <span style="background-color: '#FFFF00'">sphinx</span> of quartz", 
               "Carved symbols in a mountain hollow on the bank of an inlet irritated an eccentric person.")
    ) %>%
      mutate(text = as.character(text))

1 个答案:

答案 0 :(得分:1)

如果您可以接受简化的tags对象(而不是嵌套列表),则可以这样反复进行。还要注意,我用大写字母S代替了tags中的第一个单词。

tags <- list("Sphinx", "judge", "vow", "jackdaws", "sphinx", "love")
text <- list("Sphinx of black quartz, judge my vow", "Jackdaws love my big sphinx of quartz", 
              "Carved symbols in a mountain hollow on the bank of an inlet irritated an eccentric person.")

for (i in seq_along(tags)) {
  for (j in seq_along(text)) {
    text[[j]] <- gsub(sprintf("(%s)", tags[[i]]),
                      replacement = '<span style="background-color: #FFFF00">\\1</span>',
                      x = text[[j]])
  }
}

[[1]]
[1] "<span style=\"background-color: #FFFF00\">Sphinx</span> of black quartz, <span style=\"background-color: #FFFF00\">judge</span> my <span style=\"background-color: #FFFF00\">vow</span>"

[[2]]
[1] "Jackdaws <span style=\"background-color: #FFFF00\">love</span> my big <span style=\"background-color: #FFFF00\">sphinx</span> of quartz"

[[3]]
[1] "Carved symbols in a mountain hollow on the bank of an inlet irritated an eccentric person."