> rawToChar( charToRaw( str)[ c( TRUE, FALSE)]) [1] "ACEG" Regards
On Sat, Sep 05, 2015 at 04:59:54PM -0400, Evan Cooch wrote: > Suppose I had the following string, which has length of integer multiple of > some value n. So, say n=2, and the example string has a length of (2x4) = 8 > characters. > > str <- "ABCDEFGH" > > What I'm trying to figure out is a simple, base-R coded way (which I > heuristically call StrSubset in the following) to extract every nth > character from the string, to generate a new string. > > So > > str <- "ABCDEFGH" > > new_str <- StrSubset(str); > > print(new_str) > > which would yield > > "ACEG" > > > Best I could come up with is something like the following, where I extract > every odd character from the string: > > StrSubset <- function(string) > { > paste(unlist(strsplit(string,""))[seq(1,nchar(string),2)],collapse="") } > > > Anything more elegant come to mind? Trying to avoid regex if possible > (harder to explain to end-users), but if that meets the 'more elegant' sniff > test, happy to consider... > > Thanks in advance... > > ______________________________________________ > 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. > ______________________________________________ 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.