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@chori.org skype: jillah11 [[alternative HTML version deleted]]
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 that I get this: > >> res > [1] "astringb" "cstringd" "e" >It will be problematic if there are two "string" elements in a row, but if you can assure me that is not so, then this may suffice: > res <- vec # First do the pasting > res[grep("string", vec)] <- paste(vec[grep("string", vec)],vec[grep("string", vec)+1],sep="") # Now need to remove the strings which were concatenated to the prior ones > res <- res[-(grep('string', res)+1)] > res [1] "astringb" "cstringd" "e"> Any help is much appreciated, still learning. >As are we all. Why do you think we do this?> Many thanks, > JillDavid Winsemius, MD Heritage Laboratories West Hartford, CT
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@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@chori.org > skype: jillah11 > > > > > > > [[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. >[[alternative HTML version deleted]]
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 strings. Since you have a working solution, I won't write code... but this approach might be a little easier to scale if, for example, you need to concatenate every three elements of the vector. Best, Gray On Fri, Dec 4, 2009 at 5: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. >-- Gray Calhoun Assistant Professor of Economics Iowa State University
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 <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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O