我重现了我的问题的简单版本。我本质上想将所有语句中语句列中的英语单词替换为西班牙语等效词。
library(tidyverse)
english <- c('hello','world','my','name', 'is')
spanish <- c('hola','mundo','mi','nombre', 'es')
statement <-c('Hello my name is john doe',' hello world','my name is world','hello john, my world is','jane is my world ')
df <- data.frame(english,spanish,statement)
df
我试过了
df %>%
str_replace_all(statement, c(df$english), c(df$spanish))
和
str_replace_all(statement, c(df$english), c(df$spanish)).
第二次尝试让我更接近我的答案。仅替换了一个答案。
您可以使用
match
:
data.frame(statement = statement) |>
mutate(trad = lapply(strsplit(statement, " "), \(x)
ifelse(is.na(match(tolower(x), english)),
english,
spanish[match(tolower(x), english)]) |>
paste(collapse = " ")))
# statement trad
# 1 Hello my name is john doe hola mi nombre es is hello
# 2 hello world hello hola mundo
# 3 my name is world mi nombre es mundo
# 4 hello john, my world is hola world mi mundo es
# 5 jane is my world hello es mi mundo
我认为你正在尝试:
library(tidyverse)
english <- c("hello", "world", "my", "name", "is")
spanish <- c("hola", "mundo", "mi", "nombre", "es")
statement <- c("Hello my name is john doe", " hello world", "my name is world", "hello john, my world is", "jane is my world ")
df <- data.frame(english, spanish, statement)
rowwise(df) |> mutate(
replaced_stat = str_replace_all(
statement, fixed(english,
ignore_case = TRUE
) # or false depending ...
, spanish
)
)
但你可能想要别的东西