> > Not super slick but: > > > > by_two <- function(x, collapse = ""){ > > dim(x) <- c(length(x) / 2, 2) > > apply(x, 1, function(y) paste(y, collapse = collapse)) > > }
Does that do what you want? > by_two(letters[1:10]) [1] "af" "bg" "ch" "di" "ej" I thought that you wanted [1] "ab" "cd" "ef" "gh" "ij" In any case, I think it would be quicker to make 1 call to paste together two vectors instead of having apply make n/2 calls to paste. E.g., f_a <- function(x) { stopifnot( length(x)%%2 == 0) i <- seq(from=1, to=length(x), by=2) paste(x[i], x[i+1], sep="") } or f_b <- function(x) { stopifnot( length(x)%%2 == 0) i <- seq(from=1, to=length(x)/2) paste(x[i], x[i+length(x)/2], sep="") } depending on what result you want > f_a(letters[1:10]) [1] "ab" "cd" "ef" "gh" "ij" > f_b(letters[1:10]) [1] "af" "bg" "ch" "di" "ej" Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On > Behalf > Of Abhishek Pratap > Sent: Thursday, April 04, 2013 5:55 PM > To: R. Michael Weylandt > Cc: r-help@r-project.org > Subject: Re: [R] about subsetting vectors/list in R > > On Thu, Apr 4, 2013 at 5:53 PM, R. Michael Weylandt < > michael.weyla...@gmail.com> wrote: > > > On Thu, Apr 4, 2013 at 7:46 PM, Abhishek Pratap <abhishek....@gmail.com> > > wrote: > > > Hey Guys > > > > > > Getting spinned about a slick way to join every 2 entry in a list / > > vector > > > in R > > > > > > x=(rep(c('A','G','C','T'),1000)) > > > > > > A G C T A G C T etc > > > > > > form another list with entries as > > > AG CT AG etc > > > > Not super slick but: > > > > by_two <- function(x, collapse = ""){ > > dim(x) <- c(length(x) / 2, 2) > > apply(x, 1, function(y) paste(y, collapse = collapse)) > > } > > > > Cheers, > > MW > > > > Thanks.. just wondering if this will be slick for list/vectors with 100 > thousands of entries. ? > > -Abhi > > [[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. ______________________________________________ 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.