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
RegionTyPe | SalesRep | Sales |
City1 | A1 | 3 |
City1 | A1 | |
City1 | A1 | 4 |
City1 | A2 | 1 |
City1 | A2 | |
City1 | A2 | |
City1 | A3 | |
City1 | A3 | |
City1 | A3 | |
City2 | A3 | |
City3 | A3 | |
City4 | A3 | |
City5 | A3 | |
City6 | A3 | |
City7 | A3 | |
City8 | A4 | 3 |
City9 | A4 | 3 |
State1 | A4 | 3 |
State2 | A4 | 3 |
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" )))