3

I have the following data frame with measurements concatenated into a single column, separated by some delimiter:

df <- data.frame(v1=c(1,2), v2=c("a;b;c", "d;e;f"))
df
     v1 v2
  1  1  a;b;c
  2  2  d;e;f;g

I would like to melt/transforming it into the following format:

     v1 v2
  1  1  a
  2  1  b
  3  1  c
  4  2  d
  5  2  e
  6  2  f
  7  2  g

Is there an elegant solution?

Thx!

behas
  • 3,386
  • 5
  • 27
  • 27

1 Answers1

7

You can split the strings with strsplit.

Split the strings in the second column:

splitted <- strsplit(as.character(df$v2), ";")

Create a new data frame:

data.frame(v1 = rep.int(df$v1, sapply(splitted, length)), v2 = unlist(splitted))

The result:

  v1 v2
1  1  a
2  1  b
3  1  c
4  2  d
5  2  e
6  2  f
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168