Combinatorial letters in R

0

I have letters, for example: (abc)

I want your combinations. I need a vector with output ("a-b" "a-c" "b-c") .

I use the function:

CL<-combn(letters[1:3], 2,simplify = T)
CL.1 <-c(as.vector(CL))
CL.1

But I can not give you that way out.

Can you help me with this exit?

    
asked by toto28 09.10.2016 в 23:42
source

1 answer

0

@ toto28,

One way to do what you want is:

First, use the function to obtain the combinations. It is important not to simplify so that each combination appears as an element of a list

 CL <- combn(letters[1:3], 2, simplify = FALSE)

Then you use the lapply function in conjunction with the paste function (and its collapse = "-" argument.) This will apply the paste function for each element in the list, ie, for each combination.

CL.1 <- lapply(CL, paste, collapse = "-")

Finally, since lapply also returns a list, it can be converted into a vector with the function unlist

unlist(CL.1)
    
answered by 10.10.2016 / 06:02
source