【问题标题】:restructuring a Data.Frame Into multiple rows based on strsplit [duplicate]基于strsplit将Data.Frame重组为多行[重复]
【发布时间】:2019-06-06 20:18:39
【问题描述】:

我有这样的数据结构。

    structure(list(id = c("4031", "1040;2040;3040", "4040", 
    "1050;2050;3050"), description = c("Sentence A", 
    "Sentence B", "Sentence C", 
    "Sentence D")), row.names = 1:4, class = "data.frame")

              id description
1           4031  Sentence A
2 1040;2040;3040  Sentence B
3           4040  Sentence C
4 1050;2050;3050  Sentence D

我想重组数据,使带有“;”的 id被分成单独的行 - 我想要这样:

structure(list(id = c("4031", "1040","2040","3040", "4040", 
"1050","2050","3050"), description = c("Sentence A", 
"Sentence B","Sentence B","Sentence B", "Sentence C", 
"Sentence D","Sentence D","Sentence D")), row.names = 1:8, class = "data.frame")

   id description
1 4031  Sentence A
2 1040  Sentence B
3 2040  Sentence B
4 3040  Sentence B
5 4040  Sentence C
6 1050  Sentence D
7 2050  Sentence D
8 3050  Sentence D

我知道我可以用 strsplit 拆分 id 列,但无法找到一种有效的方法将其转换为没有循环的行

strsplit( as.character( a$id ) , ";" )

【问题讨论】:

    标签: r strsplit


    【解决方案1】:

    使用 R 基础:

    > IDs <- strsplit(df$id, ";")
    > data.frame(ID=unlist(IDs), Description=rep(df$description, lengths(IDs)))
        ID Description
    1 4031  Sentence A
    2 1040  Sentence B
    3 2040  Sentence B
    4 3040  Sentence B
    5 4040  Sentence C
    6 1050  Sentence D
    7 2050  Sentence D
    8 3050  Sentence D
    

    【讨论】:

      【解决方案2】:

      tidyr 的一个非常方便的可能性是:

      separate_rows(df, id)
      
          id description
      1 4031  Sentence A
      2 1040  Sentence B
      3 2040  Sentence B
      4 3040  Sentence B
      5 4040  Sentence C
      6 1050  Sentence D
      7 2050  Sentence D
      8 3050  Sentence D
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-22
        • 2022-01-02
        • 2012-12-28
        • 2012-06-23
        • 1970-01-01
        相关资源
        最近更新 更多