Hi,
Welcome to the RStudio community!
There ar multiple ways of doing this. Here are some examples:
#Dummy data
myData = data.frame(
id = 1:5,
value = c("A", "A", "B", "C", 'D')
)
myData
#> id value
#> 1 1 A
#> 2 2 A
#> 3 3 B
#> 4 4 C
#> 5 5 D
#Option 1
myData$value[myData$value == "A"] = "X"
#Option 2
myData[myData$value == "A", "value"] = "X"
myData
#> id value
#> 1 1 X
#> 2 2 X
#> 3 3 B
#> 4 4 C
#> 5 5 D
Created on 2022-06-06 by the reprex package (v2.0.1)
If you like to change multiple values at once you can also use the case_when() function from the dplyr package (or alternatively write longer if-else statements)
#In case of multiple changes at once
library(dplyr)
myData = data.frame(
id = 1:5,
value = c("A", "A", "B", "C", 'D')
)
myData$value = case_when(
myData$value == "A" ~ "X",
myData$value %in% c("B", "C") ~ "Y",
TRUE ~ "Z"
)
myData
#> id value
#> 1 1 X
#> 2 2 X
#> 3 3 Y
#> 4 4 Y
#> 5 5 Z
Created on 2022-06-06 by the reprex package (v2.0.1)
Hope this helps,
PJ