0

Consider this basic dataset:

myfruit <- tibble(fruit = c("apple", "orange", "apple", "pear", "banana"),
                  quantity = c("10","20","10","30", "40"))

Using R, how can I rename all values in the "fruit" column which display "apple" to "mango", without changing its associated "quantity" value? I know you can manually change the values but what is a more efficient method for larger datasets?

Any help would be much appreciated - many thanks, Karima

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
kiwi
  • 565
  • 3
  • 11
  • 2
    `myfruit$fruit[myfruit$fruit == "apple"] <- "mango"`. I don't post as an answer because I bet that this is a duplicate. – Rui Barradas Sep 28 '20 at 16:40

1 Answers1

1

You can use the vectorized ifelse function here:

myfruit$fruit <- ifelse(myfruit$fruit == "apple",
                        "mango", myfruit$fruit)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360