Dimitri Liakhovitski
2016-Dec-08 23:02 UTC
[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
David L Carlson
2016-Dec-08 23:51 UTC
[R] creating possible cominations of a vector's elements
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-bounces at r-project.org] On Behalf Of Dimitri Liakhovitski Sent: Thursday, December 8, 2016 5:03 PM To: r-help <r-help at 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 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.
Dimitri Liakhovitski
2016-Dec-08 23:58 UTC
[R] creating possible cominations of a vector's elements
Thanks a lot, David - this is great! On Thu, Dec 8, 2016 at 6:51 PM, David L Carlson <dcarlson at 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-bounces at r-project.org] On Behalf Of Dimitri Liakhovitski > Sent: Thursday, December 8, 2016 5:03 PM > To: r-help <r-help at 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 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.-- Dimitri Liakhovitski
William Dunlap
2016-Dec-09 00:05 UTC
[R] creating possible cominations of a vector's elements
> 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]]
David L Carlson
2016-Dec-09 01:12 UTC
[R] creating possible cominations of a vector's elements
This corrects an error in my earlier function definition: makestrings <- function(vec) { 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(vec[x:y], collapse=" "), x=idx[, 1], y=idx[, 2]) } David C -----Original Message----- From: David L Carlson Sent: Thursday, December 8, 2016 5:51 PM To: 'Dimitri Liakhovitski' <dimitri.liakhovitski at gmail.com>; r-help <r-help at r-project.org> Subject: RE: [R] creating possible cominations of a vector's elements 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-bounces at r-project.org] On Behalf Of Dimitri Liakhovitski Sent: Thursday, December 8, 2016 5:03 PM To: r-help <r-help at 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 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.
David L Carlson
2016-Dec-09 01:16 UTC
[R] creating possible cominations of a vector's elements
Not my day. Another correction: makestrings <- function(vec) { len <- length(vec) idx <- expand.grid(1:len, 1:len) idx <- idx[idx$Var2 <= idx$Var1, c("Var2", "Var1")] mapply(function(x, y) paste(vec[x:y], collapse=" "), x=idx[, 1], y=idx[, 2]) } David C -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of David L Carlson Sent: Thursday, December 8, 2016 7:12 PM To: Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com>; r-help <r-help at r-project.org> Subject: Re: [R] creating possible cominations of a vector's elements This corrects an error in my earlier function definition: makestrings <- function(vec) { 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(vec[x:y], collapse=" "), x=idx[, 1], y=idx[, 2]) } David C -----Original Message----- From: David L Carlson Sent: Thursday, December 8, 2016 5:51 PM To: 'Dimitri Liakhovitski' <dimitri.liakhovitski at gmail.com>; r-help <r-help at r-project.org> Subject: RE: [R] creating possible cominations of a vector's elements 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-bounces at r-project.org] On Behalf Of Dimitri Liakhovitski Sent: Thursday, December 8, 2016 5:03 PM To: r-help <r-help at 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 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. ______________________________________________ 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.