azal
azal

Reputation: 1260

Specific Permutations in R

I am trying to create permutations of the alphabet {0,1,2,3} using combinat::permn.

The thing is that I want each one of the permutations to be converted to the form of '%s-%s-%s'..etc and to be stored in a list. For example,

> library(combinat)
> permn(numbers[1:4])
[[1]]
[1] "0" "1" "2" "3"
[[2]]
[1] "0" "1" "3" "2"
 .
 .
 . and so on

But I want to convert the output for all permutations into a list of string sequences of my specific format, i.e. '0-1-2-3', '0-1-3-2 etc.

Upvotes: 0

Views: 45

Answers (1)

Dason
Dason

Reputation: 61983

Use lapply to apply paste on each of the vectors and collapse them with the delimiter you want (in this case "-").

lapply(permn(0:3), paste, collapse = "-")

If you just want the output as a vector instead of a list you could use sapply in place of lapply

Upvotes: 2

Related Questions