replace cases contain a certain word with another word and replace all other cases with a third word in R

54 Views Asked by At

My data are in a text file with about 100 columns, but for now I'm focusing on the location column. I need to change this column so that values containing "ref" should be replaced by "reference", and all other locations (that not contain "ref") should be replaced by "test".

I'm using the grepl() function for replacing all location that contain "ref", but it does not work. Also, I can't find a function that can replace "all other locations" with "test":

library(dplyr)

farmdata<- read.delim("C:/Users/Documents/farmdata.txt")

farmdata %>% mutate(location_new = case_when(grepl("ref", location) ~ "refernce"))
0

There are 0 best solutions below