[R] paste adjacent elements matching string

Jill Hollenbach jhollenbach at chori.org
Sat Dec 5 09:57:09 CET 2009


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, collapse = ",")), ",")[[1]]
> 
> It first runs all the strings together into a single comma-separate string
> using paste and then replaces each occurrence of "string," with "string"
> using gsub.  Finally it breaks the long string back up into individual
> strings using strsplit.
> 
> On Fri, Dec 4, 2009 at 8:42 PM, Jill Hollenbach
> <jillah at 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"       "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 help is much appreciated, still learning.
>>
>> Many thanks,
>> Jill
>>
>>
>>
>>
>> ________________________________________________
>>     Jill Hollenbach, PhD, MPH
>>     Associate Staff Scientist
>>     Center for Genetics
>>     Children's Hospital Oakland Research Institute
>>     jhollenbach at chori.org
>>     skype: jillah11
>>
>>
>>
>>
>>
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at 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.
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at 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.
> 
> 

-- 
View this message in context: http://n4.nabble.com/paste-adjacent-elements-matching-string-tp949036p949111.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list