I’m trying to get all possible combinations of values from a column of my data frame.
I’d like to transform this data frame so that one row has a combination (a pair) of values from this particular column.
For example, if a column from the original data has value a, b, and c, I’d like to have something like the following in the result table.
col1 | col2
a | a
a | b
a | c
b | a
b | b
b | c
c | a
c | b
c | c
Would it be possible to do that in Exploratory?
Hi Yasuyuki.
There’s a command called expand
in tidyr package. This command can give you all the data combinations of given column. You can use this to achieve it. Here’s what I did.
0). Create a base test data frame by hand. Click “Import by Writing R Script”.
At the script editor, type data.frame(col1=c("a", "b", "c"))
and hit “Get Data” button. Once you see the data frame that you want, click “Save” to import this data frame into the project.
1). Create a copy of col1 with a name col2. You can do that by mutate(c2=c1)
.
2). Issue expand
command to get all the combinations. You can do this by expand(col1, col2)
.
I shared this data with all the steps at https://exploratory.io/data/kei/8723a1448139 You can view it, or you can even download and import this data frame with all steps into your project.
Hope this helps.
–Kei
2 Likes
Thank you Kei!
Exactly what I needed.
Thank you for the example! That helped!
-Yasu