[R] creating possible cominations of a vector's elements

William Dunlap wdunlap at tibco.com
Fri Dec 9 01:05:41 CET 2016


> ij <- combn(length(x)+1, 2)
> lapply(seq_len(ncol(ij)), function(k) x[ij[1,k]:(ij[2,k]-1)])
[[1]]
[1] "this"

[[2]]
[1] "this" "is"
...
[[9]]
[1] "my"     "vector"

[[10]]
[1] "vector"


Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Dec 8, 2016 at 3:02 PM, Dimitri Liakhovitski <
dimitri.liakhovitski at gmail.com> wrote:

> 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 at 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list