0

I've got a dataset that looks like this:

col1 | col2
a | b c d
e | f g h i j

I know the permutations function from gtool :

p <- (permutations(n=4,r=2,v=df),repeats.allowed=FALSE)

can extract the different permutations with or without repetition, but I need column 1 to stay in place, so that the expected outcome would be:

a b
a c
a d
e f
e g
e h
e i
e j

The documentation doesn't specify how to accomplish something like this. The second vector is variable length and I'll be using str_extract_all to pull them out.

Has anyone seen a way to accomplish the above? The intended outcome is an edge file to be used for network graphing.

Christopher Penn
  • 539
  • 4
  • 14

1 Answers1

1

You could use tidyr::separate_rows:

library(tidyr)

df %>%
  separate_rows(col2, sep = " ")

  col1 col2
1    a    b
2    a    c
3    a    d
4    e    f
5    e    g
6    e    h
7    e    i
8    e    j

Data:

df <- read.table(text = "col1  col2
  a  'b c d'
  e  'f g h i j'",  header = TRUE, stringsAsFactors = FALSE)
Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56