is there a function which is equivalent to replace in vim?

Assume we have text file:

text <- "Quantum entanglement is a physical phenomenon that occurs when pairs or groups of particles are generated, interact, or share spatial proximity in ways such that the quantum state of each particle cannot be described independently of the state of the others, even when the particles are separated by a large distance."

I want to search "item" and replace with "code" in data frame df

df <- tibble::tribble(
                     ~item, ~code,
    "Quantum entanglement", "A01",
     "physical phenomenon", "A02",
           "quantum state", "A03",
       "quantum mechanics", "A04"
    )

in vim, i will do it one by one

%s/Quantum entanglement/A01/g
%s/physical phenomenon/A02/g
%s/quantum state/A03/g
%s/quantum mechanics/A04/g

However, if there are many item, it will be very laborious work. So I want to ask:
is there a R function which is equivalent to search & replace in vim?

May be something like this?

library(magrittr)
library(stringi)

text <- "Quantum entanglement is a physical phenomenon that occurs when pairs or groups of particles are generated, interact, or share spatial proximity in ways such that the quantum state of each particle cannot be described independently of the state of the others, even when the particles are separated by a large distance."

df <- tibble::tribble(
  ~item, ~code,
  "Quantum entanglement", "A01",
  "physical phenomenon", "A02",
  "quantum state", "A03",
  "quantum mechanics", "A04"
)

df %$%
  stri_replace_all_fixed(str = text,
                         pattern = item,
                         replacement = code,
                         vectorize_all = FALSE)
#> [1] "A01 is a A02 that occurs when pairs or groups of particles are generated, interact, or share spatial proximity in ways such that the A03 of each particle cannot be described independently of the state of the others, even when the particles are separated by a large distance."

Created on 2019-08-20 by the reprex package (v0.3.0)

1 Like

thanks for your anwers. I write a stringr version according to your idea.

library(tidyverse)
library(stringr)

text <- "Quantum entanglement is a physical phenomenon that occurs when pairs or groups of particles are generated, interact, or share spatial proximity in ways such that the quantum state of each particle cannot be described independently of the state of the others, even when the particles are separated by a large distance."


pairs <-
  tibble::tribble(
    ~item, ~code,
    "Quantum entanglement", "A01",
    "physical phenomenon", "A02",
    "quantum state", "A03",
    "quantum mechanics", "A04"
  ) %>%
  tibble::deframe()



text %>% str_replace_all(pairs)
#> [1] "A01 is a A02 that occurs when pairs or groups of particles are generated, interact, or share spatial proximity in ways such that the A03 of each particle cannot be described independently of the state of the others, even when the particles are separated by a large distance."

Created on 2019-08-20 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.