behas behas - 2 months ago 9
R Question

melt data frame and split values

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!

Answer

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
Comments