Title might be a little confusing but I think I can explain it here pretty well.
So I've looked for solutions to this today, but there was nothing similar to mine that worked.
I need to create all possible pairs of strings in a column, based on a grouping variable (the "ID" column in my case), and then also take the values from another column ("X" column), and separate those values into 2 new columns based on the string pair that was created.
The "Tissue" column is made up of strings, everything else is numeric.
X being the value for the first string in the pair, X2 being the second
My data frame is like this
| Tissue | ID | X |
|---|---|---|
| A | 1 | 2 |
| B | 1 | 3 |
| C | 1 | 7 |
| D | 2 | 4 |
| A | 2 | 2 |
| B | 2 | 3 |
| C | 2 | 7 |
And I want it to end up like this
| Tissue Pairs | ID | X | X2 |
|---|---|---|---|
| A B | 1 | 2 | 3 |
| A C | 1 | 2 | 7 |
| B C | 1 | 3 | 7 |
| A D | 2 | 2 | 4 |
| B D | 2 | 3 | 4 |
| C D | 2 | 7 | 4 |
So there will be copies of the pairs throughout the "Tissue Pairs" column, but only because they belong to different IDs
structure(list(Tissue = c("Testis", "Prostate", "Colon - Transverse",
"Whole Blood", "Kidney - Cortex", "Prostate", "Kidney - Cortex",
"Colon - Transverse", "Testis", "Kidney - Cortex"), ID = c("1",
"1", "1", "2", "2", "2", "3", "3", "3", "4"), X = c(-0.0423342169666184,
-0.0159575980119219, 0.0653906499828914, 0.0509419093203423,
-0.0164593204483257, 0.0128395479305178, -0.0214528156331502,
0.139151567409083, -0.038963750292625, -0.0182265229147926)), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"), row.names = c(NA, -10L), groups = structure(list(
ID = c("1", "2", "3", "4"), .rows = structure(list(1:3, 4:6,
7:9, 10L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -4L), .drop = TRUE))
Here is a reproducible portion of the stuff I am working with, all identifiable information removed of course.
Thank you!