I am preparing a questionnaire to ask which transport mode do the respondents use in different conditions in terms of its travel time and cost.
There are three transport modes, two levels of travel time and three levels of travel cost as below:
mode <- c(1:3)
time <- c(1:2)
cost <- c(1:3)
I would generate all combinations of travel time and cost by transport mode but do not know how to generate it easily in R
.
In the questionnaire, it shows three modes in one pair of modes with different conditions like the example below. comb
indicates combination number of each pair of modes.
comb mode time cost
1 1 1 1
1 2 1 1
1 3 1 1
2 1 1 1
2 2 2 1
2 3 2 1
3 1 2 1
3 2 1 2
3 3 1 1
4 1 1 3
4 2 2 3
4 3 1 1
5 1 1 2
5 2 2 1
5 3 1 3
6 1 1 1
6 2 1 1
6 3 1 1
7 1 1 1
7 2 1 1
7 3 1 1
8 1 1 1
8 2 1 1
8 3 1 1
..... continues till fulfilling all combinations
I used expand.grid()
but it returns just 18 combinations of mode, time and cost (3*2*3) without taking permutation by a pair of transport mode into account. I also tried several permutation functions but it may not bring my desired result. I prefer to make it in a data.frame
with grouping variable such as comb
in the example.
Permute groups in a data.frame R
How to calculate permutations of group labels with R?
It would be highly appreciated to generate all combinations simply..