Skip to content

Instantly share code, notes, and snippets.

Last active Oct 2, 2015
What would you like to do?
R - Simultaneously Replacing a set of columns using 'gsub'
colList <- c("cut", "clarity") #list of columns to replace. Can also be column numbers.
rowstoReplace <- 1:10
lapply(colList, function(colname){gsub("I", ".", diamonds[rowstoReplace, colname])})
#lapply gives it one column name at a time...
#Use a comma instead of rowstoReplace if you want all rows changed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment