Replace a cell value for a pattern in R

Hello friends! today we’ll be learning how to replace a cell value for a pattern in R

RegionTyPeSalesRepSales
City1A13
City1A1
City1A14
City1A21
City1A2
City1A2
City1A3
City1A3
City1A3
City2A3
City3A3
City4A3
City5A3
City6A3
City7A3
City8A43
City9A43
State1A43
State2A43

Code

#replace a cell value for a pattern
library(dplyr)
#load file
df <- read.csv("Test.csv")
#replace a cell value for a pattern
df1$Region <- ifelse( grepl("City",df1$RegionTyPe),"City",
ifelse( grepl("State",df1$RegionTyPe),"State",
ifelse( grepl("County",df1$RegionTyPe),"County","Others"
)))


Leave a Reply

Your email address will not be published. Required fields are marked *