Re: [R] paste adjacent elements matching string

2009-12-05 Thread Jill Hollenbach
thanks so much, this did the trick! Jill Gabor Grothendieck wrote: Try this which assumes that comma does not appear in any of the strings. You can substitute a different non-appearing character if a comma does appear in any of the strings: strsplit(gsub(string,, string, paste(vec,

Re: [R] paste adjacent elements matching string

2009-12-05 Thread Gray Calhoun
A different approach than those other people listed would be to change the vector into a matrix (by setting its dimension, if you don't need to keep the original vector) and then applying paste along one of the dimensions. Obviously, you'd need to pad the end of the vector with NAs or empty

Re: [R] paste adjacent elements matching string

2009-12-05 Thread Henrique Dallazuanna
Try this also: sapply(grep(string$, vec), function(idx)paste(vec[idx + 0:1], collapse = )) On Fri, Dec 4, 2009 at 11:42 PM, Jill Hollenbach jil...@sbcglobal.net wrote: Hi all, I would like to combine elements of a vector: vec - c(astring,  b, cstring,  d, e)   vec [1] astring b      

[R] paste adjacent elements matching string

2009-12-04 Thread Jill Hollenbach
Hi all, I would like to combine elements of a vector: vec - c(astring, b, cstring, d, e) vec [1] astring b cstring d e such that for every element that contains string at the end, it is combined with the next element, so that I get this: res [1] astringb cstringd e Any

Re: [R] paste adjacent elements matching string

2009-12-04 Thread David Winsemius
On Dec 4, 2009, at 8:42 PM, Jill Hollenbach wrote: Hi all, I would like to combine elements of a vector: vec - c(astring, b, cstring, d, e) vec [1] astring b cstring d e such that for every element that contains string at the end, it is combined with the next element, so

Re: [R] paste adjacent elements matching string

2009-12-04 Thread Gabor Grothendieck
Try this which assumes that comma does not appear in any of the strings. You can substitute a different non-appearing character if a comma does appear in any of the strings: strsplit(gsub(string,, string, paste(vec, collapse = ,)), ,)[[1]] It first runs all the strings together into a single