Skip to content

Instantly share code, notes, and snippets.

@kdaily
Created November 13, 2012 03:03
Show Gist options
  • Save kdaily/4063700 to your computer and use it in GitHub Desktop.
Save kdaily/4063700 to your computer and use it in GitHub Desktop.
Example for using plyr to replace strings in multiple columns
## Need plyr
require(plyr)
## Some fake data
foo <- data.frame(a=rep("foo", 3), b=rep("foofoo", 3), c=rep("foobarfoo", 3))
## Replace function, works on vectors
fn <- function(x) gsub("foo", "bar", x)
## Turn it into a column-wise function; only apply to cols "a" and "b"
fncol <- colwise(fn, .cols=c("a", "b"))
## Apply the function
fncol(foo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment