You may also use recode
from car
. This would work with both factor
and character
class
library(dplyr)
library(car)
res <- mutate(df1, Fruit= recode(Fruit, "'apple'='lemon'"))
res
# Fruit Cost
#1 lemon 6
#2 lemon 7
#3 orange 3
#4 orange 4
str(res)
#'data.frame': 4 obs. of 2 variables:
# $ Fruit: Factor w/ 2 levels "lemon","orange": 1 1 2 2
# $ Cost : int 6 7 3 4
Changing the class to 'character'
df1$Fruit <- as.character(df1$Fruit)
str(mutate(df1, Fruit= recode(Fruit, "'apple'='lemon'")))
#'data.frame': 4 obs. of 2 variables:
#$ Fruit: chr "lemon" "lemon" "orange" "orange"
#$ Cost : int 6 7 3 4
data
df1 <- structure(list(Fruit = structure(c(1L, 1L, 2L, 2L),
.Label = c("apple",
"orange"), class = "factor"), Cost = c(6L, 7L, 3L, 4L)),
.Names = c("Fruit",
"Cost"), row.names = c(NA, -4L), class = "data.frame")