reshape dataframe based on a string split in one column in R

前端 未结 6 1072
礼貌的吻别
礼貌的吻别 2020-12-30 12:58

I have the following data structure

ID  Type  Values
1   A     5; 7; 8
2   A     6
3   B     2; 3

and I would like to reshape it to the fol

6条回答
  •  长情又很酷
    2020-12-30 13:09

    My shot:

    a <- data.frame(id = 1:3, 
                    type = c("A", "A", "B"), 
                    values = c("5; 7; 8", "6", "2; 3"))
    
    g <- strsplit(as.character(a$values), ";")
    data.frame(id = rep(a$id, lapply(g, length)), 
                type = rep(a$type, lapply(g, length)),
                values = unlist(g))
    

提交回复
热议问题