I want to replace everything,, - , ) , ( and (space) to . from the variable DMA.NAME in the sample data frame. I referred to three posts and tried my approaches, but all failed:
Replacing column values ββin a non-list data frame
R replace all specific values ββin the data frame
Replace characters from column of data frame R
Approach 1
> shouldbecomeperiod <- c$DMA.NAME %in% c("-", ",", " ", "(", ")") c$DMA.NAME[shouldbecomeperiod] <- "."
Approach 2
> removetext <- c("-", ",", " ", "(", ")") c$DMA.NAME <- gsub(removetext, ".", c$DMA.NAME) c$DMA.NAME <- gsub(removetext, ".", c$DMA.NAME, fixed = TRUE) Warning message: In gsub(removetext, ".", c$DMA.NAME) : argument 'pattern' has length > 1 and only the first element will be used
Approach 3
> c[c == c(" ", ",", "(", ")", "-")] <- "."
Data frame example
> df DMA.CODE DATE DMA.NAME count 111 22 8/14/2014 12:00:00 AM Columbus, OH 1 112 23 7/15/2014 12:00:00 AM Orlando-Daytona Bch-Melbrn 1 79 18 7/30/2014 12:00:00 AM Boston (Manchester) 1 99 22 8/20/2014 12:00:00 AM Columbus, OH 1 112.1 23 7/15/2014 12:00:00 AM Orlando-Daytona Bch-Melbrn 1 208 27 7/31/2014 12:00:00 AM Minneapolis-St. Paul 1
I know the problem - gsub uses the template and only the first element. Two other approaches look for the whole variable for the exact value instead of searching within the value for certain characters.