Hello All, I've been using R for two years now and I am happy to say this is the first time I could not find the answer to my problem in the R-help archives. Here is the pending problem: I want to be able to insert delimiters, say commas, into a string of characters at uneven intervals such that: foo<-c("haveaniceday")#my string of character bar<-c(4,1,4,3) # my vector of uneven intervals my.fun(foo,bar) # some function that places delimiters appropriately have,a,nice,day # what the function would ideally return I've tried multiple for-loops using cut and paste but have not had success. Thanks! Chris Marcum UCI Sociology
Try this:> paste(read.fwf(textConnection(foo), bar, as.is = TRUE), collapse = ",")[1] "have,a,nice,day" On 6/18/07, Christopher Marcum <cmarcum at uci.edu> wrote:> Hello All, > > I've been using R for two years now and I am happy to say this is the > first time I could not find the answer to my problem in the R-help > archives. Here is the pending problem: > > I want to be able to insert delimiters, say commas, into a string of > characters at uneven intervals such that: > > foo<-c("haveaniceday")#my string of character > bar<-c(4,1,4,3) # my vector of uneven intervals > > my.fun(foo,bar) # some function that places delimiters appropriately > > have,a,nice,day # what the function would ideally return > > I've tried multiple for-loops using cut and paste but have not had success. > > Thanks! > Chris Marcum > UCI Sociology > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >
This should work for you.> foo<-c("haveaniceday")#my string of character > bar<-c(4,1,4,3) # my vector of uneven intervals > my.function <- function(foo, bar){+ # construct a matrix with start/end character positions + start <- head(cumsum(c(1, bar)), -1) # delete last one + sel <- cbind(start=start, + end=start + bar -1) + strings <- apply(sel, 1, function(x) substr(foo, x[1], x[2])) + paste(strings, collapse=',') + }> > my.function(foo, bar)[1] "have,a,nice,day"> >On 6/18/07, Christopher Marcum <cmarcum@uci.edu> wrote:> > Hello All, > > I've been using R for two years now and I am happy to say this is the > first time I could not find the answer to my problem in the R-help > archives. Here is the pending problem: > > I want to be able to insert delimiters, say commas, into a string of > characters at uneven intervals such that: > > foo<-c("haveaniceday")#my string of character > bar<-c(4,1,4,3) # my vector of uneven intervals > > my.fun(foo,bar) # some function that places delimiters appropriately > > have,a,nice,day # what the function would ideally return > > I've tried multiple for-loops using cut and paste but have not had > success. > > Thanks! > Chris Marcum > UCI Sociology > > ______________________________________________ > R-help@stat.math.ethz.ch 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]]
On Mon, 2007-06-18 at 16:54 -0700, Christopher Marcum wrote:> Hello All, > > I've been using R for two years now and I am happy to say this is the > first time I could not find the answer to my problem in the R-help > archives. Here is the pending problem: > > I want to be able to insert delimiters, say commas, into a string of > characters at uneven intervals such that: > > foo<-c("haveaniceday")#my string of character > bar<-c(4,1,4,3) # my vector of uneven intervals > > my.fun(foo,bar) # some function that places delimiters appropriately > > have,a,nice,day # what the function would ideally return > > I've tried multiple for-loops using cut and paste but have not had success. > > Thanks! > Chris Marcum > UCI SociologyOne more variation on the replies already provided: foo <- c("haveaniceday") bar <- c(4, 1, 4, 3) insert.char <- function(x, at, char = ",") { cs.at <- cumsum(at) vec <- unlist(strsplit(x, "")) for (i in seq(length(cs.at) - 1)) vec <- append(vec, char, cs.at[i] + i - 1) paste(vec, collapse = "") }> insert.char(foo, bar)[1] "have,a,nice,day" See ?append HTH, Marc Schwartz