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 wrote:
> Hi all,
> I would like to combine elements of a vector:
>
> vec <- c("astring", "b", "cstring", "d", "e")
> > vec
> [1] "astring" "b"
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 string
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(
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 sin
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 th
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] "ast
6 matches
Mail list logo