-2

I have a data set:

           x y z
1      apple a 4
2     orange d 3
3     banana b 2
4 strawberry c 1

How can I change the name "banana" to "grape"? I want to get:

           x y z
1      apple a 4
2     orange d 3
3     grape  b 2
4 strawberry c 1

Reproducible code:

example<-data.frame( x = c("apple", "orange", "banana", "strawberry"), y = c("a", "d", "b", "c"), z = c(4:1) )
halo09876
  • 2,725
  • 12
  • 51
  • 71
  • It depends if `x` is a factor or a character. In either way, this is very basic question that been probably asked hundreds of times on SO before. In other words, did you Google it? – David Arenburg Oct 23 '19 at 19:05
  • Convert the column to `character` class and use `example$x[example$x == "banana"] <- "grape"` – akrun Oct 23 '19 at 19:05

1 Answers1

0

Below is the solution using tidyverse in R

library(tidyverse)
example %>% 
  mutate(x = as.character(x)) %>% 
  mutate(x = replace(x, x == 'banana', 'grape'))

ashwin agrawal
  • 1,603
  • 8
  • 16