Here's a simple vector:
vec <- c('foo', 'bar', 'baz');
I want to add the suffixes _X
and _Y
to each element of this vector, so that I get a vector with six elements: c('foo_X', 'foo_Y', 'bar_X', 'bar_Y', 'baz_X', 'baz_Y')
.
I hoped that I could achieve that with sapply
:
sapply(vec, function(elem) {
c(paste0(elem, '_X'),
paste0(elem, '_Y'))
});
However, this returns a matrix rather than a vector:
foo bar baz
[1,] "foo_X" "bar_X" "baz_X"
[2,] "foo_Y" "bar_Y" "baz_Y"
What can I do to create the vector I want?