I'm trying to develop this function so that I can efficiently generate all possibile combinations of the strings.
So I have certain roots, prefixes, and sufixes. I also have different combinations of the data, some with two strings (roots, prefix) and others with three strings (roots, prefix, suffix) roots <- c("car insurance", "auto insurance") roots2 <- c("insurance") prefix <- c("cheap", "budget") prefix2 <- c("low cost") suffix <- c("quote", "quotes") suffix2 <- c("rate", "rates") suffix3 <- c("comparison") Here are just a few of the combinations I'm trying to generate. # prefix, roots, suffix # prefix, roots, suffix2 # prefix, roots, suffix3 # prefix2, roots, suffix # prefix2, roots, suffix2 # prefix2, roots, suffix3 # prefix, roots2, suffix # prefix, roots2, suffix2 # prefix, roots2, suffix3 # prefix2, roots2, suffix # prefix2, roots2, suffix2 # prefix2, roots2, suffix3 # prefix, roots # prefix2, roots # prefix, roots2 # prefix2, roots2 # roots, suffix # roots, suffix2 # roots, suffix3 # roots2, suffix # roots2, suffix2 # roots2, suffix3 # state, roots # city, roots # cityst, roots So instead of two functions for ones with two vs three parameters, I'm wondering if it's possible to just develop one function. one <- function(x, y) { nu <- do.call(paste, expand.grid(x, y)) mydf <- data.frame(nu) } two <- function(x, y, z){ mu <- do.call(paste, expand.grid(x, y, z)) mydf2 <- data.frame(mu) } On Tue, Jun 7, 2011 at 6:54 PM, Dennis Murphy <djmu...@gmail.com> wrote: > Alas, you don't have a suffix2 object defined, but try this: > > d1 <- one(prefix, roots) > d2 <- one(roots, suffix) > rbind(d1, d2) > > To see a potential flaw in your function (as least as far as console > output is concerned), try > rbind(d1, one(roots, suffix)) > > HTH, > Dennis > > On Tue, Jun 7, 2011 at 3:30 PM, Abraham Mathew <abra...@thisorthat.com> > wrote: > > Let's say that I'm trying to write a functions that will allow me to > > automate a process > > where I examine all possible combinations of various string groupings. > Each > > time I run > > the one function, I want to include the new values to the end of a data > > frame. The data > > frame will basically be one column with a lot of rows. > > > > roots <- c("car insurance", "auto insurance") > > prefix <- c("cheap", "budget") > > suffix <- c("rate", "rates") > > > > one <- function(x, y, z=0) { > > nu <- do.call(paste, expand.grid(x, y, z)) > > mydf <- data.frame(nu) > > print(mydf) > > } > > > > one(roots, suffix2) > > one(prefix, roots) > > one(prefix, roots, suffix2) > > > > The code above just replaces each value in the data frame each time I run > > the one function. > > > > How can I add the new values to the end of the data frame? > > > > > > Help! > > > > I'm running R 2.13 on Ubuntu 10.10 > > WebRep > > Overall rating > > > > [[alternative HTML version deleted]] > > > > ______________________________________________ > > R-help@r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, reproducible code. > > > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.