Thanks a lot, David - this is great! On Thu, Dec 8, 2016 at 6:51 PM, David L Carlson <dcarl...@tamu.edu> wrote: > You can use expand.grid() and mapply(): > > mystring <- "this is my vector" > mystring.spl <- strsplit(mystring, " ")[[1]] > > makestrings <- function(x) { > len <- length(mystring.spl) > idx <- expand.grid(1:len, 1:len) > idx <- idx[idx$Var2 <= idx$Var1, c("Var2", "Var1")] > mapply(function(x, y) paste(mystring.spl[x:y], collapse=" "), > x=idx[, 1], y=idx[, 2]) > } > makestrings(mystring.spl) > > [1] "this" "this is" "this is my" > [4] "this is my vector" "is" "is my" > [7] "is my vector" "my" "my vector" > [10] "vector" > > This makes a vector of strings but if you want a list use as.list(mapply()) > > David L. Carlson > Department of Anthropology > Texas A&M University > > > > -----Original Message----- > From: R-help [mailto:r-help-boun...@r-project.org] On Behalf Of Dimitri > Liakhovitski > Sent: Thursday, December 8, 2016 5:03 PM > To: r-help <r-help@r-project.org> > Subject: [R] creating possible cominations of a vector's elements > > Hello! > > I have a vector of strings 'x' that was based on a longer string > 'mystring' (the actual length of x is unknown). > > mystring <- "this is my vector" > x <- strsplit(mystr, " ")[[1]] > > I am looking for an elegant way of creating an object (e.g., a list) > that contains the following strings: > > "this" > "this is" > "this is my" > "this is my vector" > "is" > "is my" > "is my vector" > "my" > "my vector" > "vector" > > Thanks a lot! > > -- > Dimitri > > ______________________________________________ > R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
-- Dimitri Liakhovitski ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.