Pasting two vectors with combinations of all vectors' elements
Another option using sprintf
in combination with expand.grid
:
eg <- expand.grid(vis, vars)
sprintf('%s.%s', eg[,2], eg[,1])
which gives:
[1] "SR.1" "SR.2" "SR.3" "PL.1" "PL.2" "PL.3"
Explanation:
- With
expand.grid
you create all combinations of the two vectors. sprintf
pastes the two vectors together according to the specified format ('%s.%s'
). Each%s
part of the format is replaced by the elements of the vectors.
You can use this, but there may be a simpler solution :
R> apply(expand.grid(vars, vis), 1, paste, collapse=".")
[1] "SR.1" "PL.1" "SR.2" "PL.2" "SR.3" "PL.3"
expand.grid
gives back a data.frame
which when used with apply
, apply
will convert it to a matrix
. This is just unnecessary (and inefficient on large data). outer
gives a matrix
and also takes function argument. It'll be much efficient on huge data as well.
Using outer
:
as.vector(outer(vars, vis, paste, sep="."))
# [1] "SR.1" "PL.1" "SR.2" "PL.2" "SR.3" "PL.3"
This old question already has an accepted answer. But as it's being used as dupe target, I believe it's worthwhile to add a data.table
solution which uses the cross join function CJ()
:
library(data.table)
options(datatable.CJ.names=FALSE) # required with version version 1.12.0+
CJ(vars, vis)[, paste(V1, V2, sep =".")]
#[1] "PL.1" "PL.2" "PL.3" "SR.1" "SR.2" "SR.3"
In case the original order is important:
CJ(vars, vis, sorted = FALSE)[, paste(V1, V2, sep =".")]
#[1] "SR.1" "SR.2" "SR.3" "PL.1" "PL.2" "PL.3"
Edit: CJ()
has changed default behaviour with version 1.12.0
As announced in the release notes of version 1.12.0 (Point 3) the default option options(datatable.CJ.names=TRUE)
has changed. CJ()
now auto-names its inputs exactly as data.table()
does.
So, the code above has to be amended for data.table
version 1.12.0 and above:
library(data.table) ### version 1.12.0+
CJ(vars, vis)[, paste(vars, vis, sep =".")]
and
CJ(vars, vis, sorted = FALSE)[, paste(vars, vis, sep =".")]
resp.
Another option is to use the each
argument of rep
:
paste(rep(vars, each = length(vis)), vis, sep = ".")
I find this more straightforward than the solutions based on apply
or expand.grid
.