Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

melt data frame and split values

Tags:

r

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!

like image 899
behas Avatar asked Dec 06 '25 04:12

behas


1 Answers

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
like image 106
Sven Hohenstein Avatar answered Dec 07 '25 18:12

Sven Hohenstein