-1

I use a dataset with string characters that contains some wrong translations. One column shows the words in the original language ("name.french"). In the next column their translations are listed ("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?

R-User
  • 47
  • 6
  • 1
    Please make the question [more reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – NelsonGon Mar 03 '20 at 15:25
  • 1
    Try it with `ifelse`, which is vectorised and designed for this sort of thing - perhaps `name.english <- ifelse(name.french == "framboise", "raspberry", name.english)` – Andrew Gustar Mar 03 '20 at 15:28

3 Answers3

4

If your data are stored in two separate vectors, you can use 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:

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)

Giovanni Colitti
  • 1,982
  • 11
  • 24
3

Use ifelse. Assuming your data.frame is called df and you want to make changes to the name.english column:

df$name.english = ifelse(name.french == 'framboise', str_replace(name.english, "rasperry", "rasberry"), df$name.english)
Giovanni Colitti
  • 1,982
  • 11
  • 24
Shivam Sarin
  • 551
  • 1
  • 7
  • 20
0

How about good old gsub(...) ie a regexbased solution for your hypothetical data.frame df.

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. 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.

GWD
  • 1,387
  • 10
  • 22