简体   繁体   中英

How do I replace the string exactly in dataset

I have this vector

x<- c("Store Leader", "Produce Lead", "Stock Lead", "Assitance Leader")

I want to replace every job title with the word Lead with just "Lead"

I have been trying this without success

x2 <- gsub("\\<Lead\\>","Lead",x, ignore.case = TRUE)

The result should look like this: x<- c("Lead", "Store Leader", "Lead", "PH Leader")

Thanks

This works the way you want, since it seems "Lead" will always occur at the end of the string.

x<- c("Store Leader", "Produce Lead", "Stock Lead", "Assitance Leader")

x2 <- sub(".*Lead$", "Lead", x, ignore.case = TRUE)

str(x2)

chr [1:4] "Store Leader" "Lead" "Lead" "Assitance Leader"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM