melt data frame and split values

后端 未结 1 1023
借酒劲吻你
借酒劲吻你 2021-01-20 15:10

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         


        
相关标签:
1条回答
  • 2021-01-20 15:47

    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
    
    0 讨论(0)
提交回复
热议问题