Having previously applied the function gsub() over an R data frame, I found that I not only want a data frame back but also one that has both numeric and character columns (in this case, converting some character to numeric.) I tinkered with trying to operate on target columns directly in the data frame, but decided to work on them separately, and then put them back together:
# with a parent data frame containing all columns, "data," and # one containing only columns of interest, "targets"... #remove punctuation and spaces from targets, and make numeric targets.clean <- as.data.frame(sapply(targets,function(x) as.numeric(gsub("[[:blank:]]","",gsub("[[:punct:]]","",x))))) #replace character columns with corresponding numeric to parent data frame data.notargets <- data[,!(names(data) %in% names(targets.clean))] data <- cbind(data.notargets,targets.clean)
Note: used classes for regular expressions here, but not advisable if just crawling over unknown variables.