Search-and-replace an element in a data frame given a list of replacements.
Code:
testing123tmp <- data.frame(x=c("it's", "not", "working"))
testing123tmp$x <- as.character(testing123tmp$x)
tmp <- list("it's" = "hey", "working"="dead")
apply(testing123tmp,2,function(x) gsubfn('.', tmp, x))
Expected Output:
x
[1,] hey
[2,] not
[3,] dead
My current output:
x
[1,] "it's"
[2,] "not"
[3,] "working"
Been looking around for possible solution in chartr and gsub, but would like simplicity (short coding) given multiple gsub is required for such operation. Also my variable tmp can be scaled to many-pair replacement such that:
tmp <- list("it's" = "hey",
"working"="dead",
"other" = "other1",
.. = .. ,
.. = .. ,
.. = .. )
Edit/Update #1:
- would also like solution in gsubfn above and data-framed