I am trying to look at some combinations. However, when I run the code that generates the combinations, the output looks like it is a matrix. I have tried to convert the matrix to a dataframe and it either crashes R or combines everything into one column. I was wondering if anyone knew how to fix this.
Here is an example dataset
structure(list(V1 = structure(1:3, .Label = c("a", "b", "c"), class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
Here is the code that I am using to create the combinations
library(arrangements)
two <- (combinations(b$V1, 2))
Here is what I was using to try and convert the data into a dataframe but everything just get combined into one column
two <- as.data.frame((combinations(b$V1, 2)))
So for this example above, I was hoping that the output would have two columns and three rows
I really need the output in a dataframe because I need to do some more downstream work on the output of the combinations
CodePudding user response:
We may need as a list
as.data.frame(combn(as.character(b$V1), 2, simplify = FALSE))
In the OP's post, the column is factor
. It may need to be converted to character
as.data.frame(combinations(as.character(b$V1), 2))
V1 V2
1 AL033528.3 THRAP3
2 AL033528.3 AC106707.1
3 AL033528.3 LIPH
4 THRAP3 AC106707.1
5 THRAP3 LIPH
6 AC106707.1 LIPH
CodePudding user response:
Another solution:
data.frame(
V1=t(combn(b$V1,2))[,1],
V2=t(combn(b$V1,2))[,2])