Permutation with two vectors

Hi all,

I am trying to get the all possible permutations from two categories. The raw data is:

3 categories as labels: Penalty No-penalty Money Fine
2 categories Yes or No that could be assigned to any of the categories. For example:

Penalty No-Penalty Money fine
Y Y N
N N Y

I am using this code:

s <- c('penalty','no penalty','money fine')
n <- c('Y','N')
nrow(permutations(n=3,r=3,v=s,repeats.allowed=F ))
Number of permutations should be: 6

To get the combinations, I use this code:
f<- apply(expand.grid(s, n), 1, paste, collapse=".")
result <- t(combn(f,4))
result

The result I get is no correct: It is giving me a mix of all categories with Y and No.

The final table should look like this:
Penalty No-Penalty Money fine
Y N Y
Y Y N
Y N N
N Y Y
N Y N
N N Y

Could you please advise on the code to create this summary table?

Hi, can you please format your code as code? Also, it'll be helpful if you can make your question into a reprex:

Right now it is difficult for me to see what you want exactly since as I read it, you have 4 groups with money, not 3. But in general, you can take a look at purrr::cross:

library(magrittr)

purrr::cross2(letters[1:3], LETTERS[1:3]) %>%
  purrr::map_chr(purrr::lift(paste, sep = "."))
#> [1] "a.A" "b.A" "c.A" "a.B" "b.B" "c.B" "a.C" "b.C" "c.C"

Created on 2018-11-19 by the reprex package (v0.2.1)

1 Like

Are you seeking for all the combination of Y and N ?
I have this:

s <- c('penalty','no penalty','money fine')
n <- c('Y','N')

setNames(expand.grid(n, n, n), s)
#>   penalty no penalty money fine
#> 1       Y          Y          Y
#> 2       N          Y          Y
#> 3       Y          N          Y
#> 4       N          N          Y
#> 5       Y          Y          N
#> 6       N          Y          N
#> 7       Y          N          N
#> 8       N          N          N

Created on 2018-11-19 by the reprex package (v0.2.1)

It is more a guess as we need a reprex as @mishabalyasin explained. Or at least a more precise output wish. Thank !

Hi Misha,

Thank you for your reply.

Apologies for my poorly presented code. I am fairly new using R.

The idea is to seek for all Y and N combinations for penalty, no penalty and money fine.

In the below post from Chirstophe, the following code provides the structure that I require.

s <- c('penalty','no penalty','money fine')
n <- c('Y','N')

setNames(expand.grid(n, n, n), s)

#>   penalty no penalty money fine

#> 1       Y          Y          Y
#> 2       N          Y          Y
#> 3       Y          N          Y
#> 4       N          N          Y
#> 5       Y          Y          N
#> 6       N          Y          N
#> 7       Y          N          N
#> 8       N          N          N

However, when I add a new category in vector s, I get only 16 combinations instead of 26. I wonder what should I add in this code for making work?

s <- c('penalty','no penalty','money fine', 'Community_service')
n <- c('Y','N')

setNames(expand.grid(n, n, n,n), s)

Hi Christophe,

Thank you so much for providing this code.

It works perfectly for 3 categories in vector s. However, I tried to replicate the same with 4 categories and I am only getting 16 combinations out of 26.

s <- c('penalty','no penalty','money fine', 'Community_service')

n <- c('Y','N')

setNames(expand.grid(n, n, n,n), s)

   penalty no penalty money fine Community_service

1        Y          Y          Y                 Y
2        N          Y          Y                 Y
3        Y          N          Y                 Y
4        N          N          Y                 Y
5        Y          Y          N                 Y
6        N          Y          N                 Y
7        Y          N          N                 Y
8        N          N          N                 Y
9        Y          Y          Y                 N
10       N          Y          Y                 N
11       Y          N          Y                 N
12       N          N          Y                 N
13       Y          Y          N                 N
14       N          Y          N                 N
15       Y          N          N                 N
16       N          N          N                 N


nrow(permutations(n=4,r=4,v=s,repeats.allowed=F))
24

Should I an extra command to your existing code for the rest of the combinations to show up?.

Regards,
Yos

If you are looking for a table with all the permutations possible for yes and no repeated 4 times as you want 4 columns at the end, I believe there are no missing data

s <- c('penalty','no penalty','money fine', 'Community_service')
n <- c('Y','N')

gtools::permutations(n=2,r=4,v=n,repeats.allowed=TRUE)
#>       [,1] [,2] [,3] [,4]
#>  [1,] "N"  "N"  "N"  "N" 
#>  [2,] "N"  "N"  "N"  "Y" 
#>  [3,] "N"  "N"  "Y"  "N" 
#>  [4,] "N"  "N"  "Y"  "Y" 
#>  [5,] "N"  "Y"  "N"  "N" 
#>  [6,] "N"  "Y"  "N"  "Y" 
#>  [7,] "N"  "Y"  "Y"  "N" 
#>  [8,] "N"  "Y"  "Y"  "Y" 
#>  [9,] "Y"  "N"  "N"  "N" 
#> [10,] "Y"  "N"  "N"  "Y" 
#> [11,] "Y"  "N"  "Y"  "N" 
#> [12,] "Y"  "N"  "Y"  "Y" 
#> [13,] "Y"  "Y"  "N"  "N" 
#> [14,] "Y"  "Y"  "N"  "Y" 
#> [15,] "Y"  "Y"  "Y"  "N" 
#> [16,] "Y"  "Y"  "Y"  "Y"

setNames(expand.grid(n, n, n, n), s)
#>    penalty no penalty money fine Community_service
#> 1        Y          Y          Y                 Y
#> 2        N          Y          Y                 Y
#> 3        Y          N          Y                 Y
#> 4        N          N          Y                 Y
#> 5        Y          Y          N                 Y
#> 6        N          Y          N                 Y
#> 7        Y          N          N                 Y
#> 8        N          N          N                 Y
#> 9        Y          Y          Y                 N
#> 10       N          Y          Y                 N
#> 11       Y          N          Y                 N
#> 12       N          N          Y                 N
#> 13       Y          Y          N                 N
#> 14       N          Y          N                 N
#> 15       Y          N          N                 N
#> 16       N          N          N                 N

Created on 2018-11-20 by the reprex package (v0.2.1)

permutations(n=4,r=4,v=s,repeats.allowed=F) is not the same as the source vector is 4 in length and the target is four also. c("Y", "F") is 2 in length - so length combination possible.

2 Likes

You are amazing Christophe. Thank you so much.

Glad it works for you !

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.