Great! Works like a charm! Thanks Petr!!
On Fri, Apr 13, 2012 at 9:03 AM, Petr Savicky [via R] < ml-node+s789695n4554013...@n4.nabble.com> wrote: > On Thu, Apr 12, 2012 at 01:14:27PM -0700, Bart6114 wrote: > > > Hey all, > > > > I am trying to generate a number of vectors with string combinations. > The > > below code solves my initial problem, however I would prefer not > hardcoding > > the positions of the 'objects' as in the future the number of 'objects' > > could increase. Is there a way to loop over these objects and generate > the > > same output? The same goes for the 'maxspacer'. > > > > Thanks a whole bunch! > > > > Bart > > > > code: > > > > objects<-c('OCT','ASS','SUP') > > maxspacer<-c(2,3) > > > > for(x in 0:maxspacer[1]){ > > for(y in 0:maxspacer[2]){ > > > > combo<-c(objects[1],rep('',x),objects[2],rep('',y),objects[3]) > > > > print(combo) > > } > > } > > Hi. > > Try the following. > > objects<-c('OCT','ASS','SUP') > maxspacer<-c(2,3) > > expand <- function(obj, sp) > { > i <- cumsum(c(1, sp+1)) > x <- rep("", times=max(i)) > x[i] <- obj > x > } > > lstsp <- lapply(maxspacer, function(x) 0:x) > spacing <- as.matrix(rev(expand.grid(rev(lstsp)))) > > for (i in seq.int(length=nrow(spacing))) { > print(expand(objects, spacing[i,])) > } > > [1] "OCT" "ASS" "SUP" > [1] "OCT" "ASS" "" "SUP" > [1] "OCT" "ASS" "" "" "SUP" > [1] "OCT" "ASS" "" "" "" "SUP" > [1] "OCT" "" "ASS" "SUP" > [1] "OCT" "" "ASS" "" "SUP" > [1] "OCT" "" "ASS" "" "" "SUP" > [1] "OCT" "" "ASS" "" "" "" "SUP" > [1] "OCT" "" "" "ASS" "SUP" > [1] "OCT" "" "" "ASS" "" "SUP" > [1] "OCT" "" "" "ASS" "" "" "SUP" > [1] "OCT" "" "" "ASS" "" "" "" "SUP" > > The functions rev() may be removed, if the order of the > output may not be exactly as in your code. > > Hope this helps. > > Petr Savicky. > > ______________________________________________ > [hidden email] <http://user/SendEmail.jtp?type=node&node=4554013&i=0>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. > > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > > http://r.789695.n4.nabble.com/Generate-combination-of-strings-tp4552952p4554013.html > To unsubscribe from Generate combination of strings, click > here<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=4552952&code=YmFydHNtZWV0czg2QGdtYWlsLmNvbXw0NTUyOTUyfC03NjAwOTE0MDU=> > . > NAML<http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml> > -- *Bart Smeets* *+32-(0)492082071* bartsmeet...@gmail.com -- View this message in context: http://r.789695.n4.nabble.com/Generate-combination-of-strings-tp4552952p4554047.html Sent from the R help mailing list archive at Nabble.com. [[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.