• r

Munging with gsub and grepl

Manipulating values based on a condition: NA is a special case, and can be referenced using is.na. Gsub is a method of substiution, while grepl returns a logical vector if match occurs or not.

NA

#replace NA with 0
df$column[is.na(df$column)] <- 0

gsub

#search for "yes", replace with "maybe" in df$column
df$column<-gsub(pattern = "yes", replacement = "maybe", df$column)

#remove everything before and including "#" in df$column
df$column<-gsub("^.*\\#", "", df$column)

#remove everything after ":"
df$column<-gsub( ":.*$", "", df$column)

#remove everything before space
df$column<-gsub(".* ", "", df$column)

#remove bracket
df$column<-gsub("\\[|\\]","",df$column)

#remove "." from column names of dataframe df
colnames(df) <- gsub("\\.","",colnames(df))

grepl

#where "_A" in df$column, replace with "rats"
df$column[grepl("_A",df$column,ignore.case=FALSE)]<-"rats"

#where not "_A" in df$column, replace with "cats"
df$column[!grepl("_A",df$column,ignore.case=FALSE)]<-"cats"