Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Last active October 2, 2015 20:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ram-N/d972c5963df2f1ce692e to your computer and use it in GitHub Desktop.
Save Ram-N/d972c5963df2f1ce692e to your computer and use it in GitHub Desktop.
R - Simultaneously Replacing a set of columns using 'gsub'
library(ggplot2)
head(diamonds,10)
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