[英]How to replace a word by another using if statement and str_replace in r
I use a dataset with string characters that contains some wrong translations.我使用包含一些错误翻译的字符串字符数据集。 One column shows the words in the original language ("name.french").一列显示原始语言中的单词(“name.french”)。 In the next column their translations are listed ("name.english").在下一列中列出了他们的翻译(“name.english”)。 Now I want to use the following command to replace the wrong translations with the correct ones:现在我想使用以下命令用正确的翻译替换错误的翻译:
if(name.french == "framboise"){name.english = str_replace(name.english, "rasperry", "rasberry");}
However, I always get the following error message: Argument cannot be interpreted as logical value.但是,我总是收到以下错误消息:参数不能解释为逻辑值。 Is there another way of replacing some wrong translations?还有另一种方法可以替换一些错误的翻译吗?
Use ifelse
.使用ifelse
。 Assuming your data.frame is called df and you want to make changes to the name.english column:假设您的 data.frame 被称为 df 并且您想对 name.english 列进行更改:
df$name.english = ifelse(name.french == 'framboise', str_replace(name.english, "rasperry", "rasberry"), df$name.english)
If your data are stored in two separate vectors, you can use ifelse
:如果您的数据存储在两个单独的向量中,您可以使用ifelse
:
name.french <- c("framboise", "not framboise")
name.english <- c("rasperry", "rasperry")
name.english2 <-
ifelse(
name.french == "framboise",
str_replace(name.english, "rasperry", "rasberry"),
name.english
)
This also works if your data are stored in a tibble
or data.frame
and you want to use tidyverse
verbs:这也适用,如果你的数据存储在一个tibble
或data.frame
并且要使用tidyverse
动词:
library(tidyverse)
d <- tibble(name.french = c("framboise", "not framboise"),
name.english = c("rasperry", "rasperry"))
d2 <- d %>%
mutate(name.english = ifelse(
name.french == "framboise",
str_replace(name.english, "rasperry", "rasberry"),
name.english
))
d
#> # A tibble: 2 x 2
#> name.french name.english
#> <chr> <chr>
#> 1 framboise rasperry
#> 2 not framboise rasperry
d2
#> # A tibble: 2 x 2
#> name.french name.english
#> <chr> <chr>
#> 1 framboise rasberry
#> 2 not framboise rasperry
Created on 2020-03-03 by the reprex package (v0.3.0)由reprex 包(v0.3.0) 于 2020 年 3 月 3 日创建
How about good old gsub(...)
ie a regex
based solution for your hypothetical data.frame df
.好的旧gsub(...)
怎么样,即您假设的 data.frame df
基于regex
的解决方案。
df[df[["name.french"]] == "framboise", "name.english"] <- gsub( pattern = "rasperry", replacement = "rasberry", x = df[df[[name.french == "framboise"]], "name.english"] )
Of cource u can easily build that into a simple function where the column name here: "name.french", the row filter criteria here: "framboise" and the pattern and replacement strings can be passed to as parameters.当然,您可以轻松地将其构建到一个简单的函数中,其中列名:“name.french”,行过滤条件:“framboise”以及模式和替换字符串可以作为参数传递给。 In case you just want to globally replace "rasperry" with "rasberry" in all of name.english
than you can drop the row filter in the df
ie the df[["name.french"]] == "framboise", ...
part of the code.如果您只想在所有name.english
“rasperry”全局替换为“rasberry”,那么您可以删除df
的行过滤器,即df[["name.french"]] == "framboise", ...
部分代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.