代码之家  ›  专栏  ›  技术社区  ›  thiagoveloso

从数据表中的JSON列分配类别

  •  4
  • thiagoveloso  · 技术社区  · 1 年前

    我有一个 data.table 看起来像 dt (在末尾提供),其中包括列 EI CutsLabsCV .

    CutsLabsCV 是一个由两个值组成的JSON,表示应用于对我的数据集进行分类的切割和标签。

    我需要创建一个新专栏 EIRange ,对 工程安装 基于上提供的切割和标签的列 CutsLabsCV .

    预期结果应该是:

              EI                                    CutsLabsCV EIRange
           <num>                                        <list>  <fctr>
     1: 101.9163 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     2: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     3: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     4: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     5: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     6: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     7: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     8: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     9: 109.5220 ["-Inf",85,120,"Inf"],["<85","85-120",">120"]  85-120
    10: 109.5220 ["-Inf",85,120,"Inf"],["<85","85-120",">120"]  85-120
    

    我怎样才能做到这一点 data.table ?我需要最有效的解决方案,因为我的真实数据集超过了2M行。

    样本数据集:

    dt <- data.table(EI = c(101.91625, 122.03178865, 122.03178865,
                            122.03178865, 122.03178865, 122.03178865, 122.03178865, 122.03178865,
                            109.521980125, 109.521980125),
                     CutsLabsCV = list(c("[\"-Inf\",90,130,\"Inf\"]", "[\"<90\",\"90-130\",\">130\"]"),
                                       c("[\"-Inf\",90,130,\"Inf\"]", "[\"<90\",\"90-130\",\">130\"]"),
                                       c("[\"-Inf\",90,130,\"Inf\"]", "[\"<90\",\"90-130\",\">130\"]"),
                                       c("[\"-Inf\",90,130,\"Inf\"]", "[\"<90\",\"90-130\",\">130\"]"),
                                       c("[\"-Inf\",90,130,\"Inf\"]", "[\"<90\",\"90-130\",\">130\"]"),
                                       c("[\"-Inf\",90,130,\"Inf\"]", "[\"<90\",\"90-130\",\">130\"]"),
                                       c("[\"-Inf\",90,130,\"Inf\"]", "[\"<90\",\"90-130\",\">130\"]"),
                                       c("[\"-Inf\",90,130,\"Inf\"]", "[\"<90\",\"90-130\",\">130\"]"),
                                       c("[\"-Inf\",85,120,\"Inf\"]", "[\"<85\",\"85-120\",\">120\"]"),
                                       c("[\"-Inf\",85,120,\"Inf\"]", "[\"<85\",\"85-120\",\">120\"]")))
    
    2 回复  |  直到 1 年前
        1
  •  3
  •   Waldi    1 年前

    一种可能的解决方案:

    dt[,EIRange:=sapply(CutsLabsCV,\(x) jsonlite::fromJSON(x[2])[2])]
    dt
    
    #           EI                                    CutsLabsCV EIRange
    #        <num>                                        <list>  <char>
    #  1: 101.9163 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
    #  2: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
    #  3: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
    #  4: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
    #  5: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
    #  6: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
    #  7: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
    #  8: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
    #  9: 109.5220 ["-Inf",85,120,"Inf"],["<85","85-120",">120"]  85-120
    # 10: 109.5220 ["-Inf",85,120,"Inf"],["<85","85-120",">120"]  85-120
    
        2
  •  2
  •   Friede    1 年前

    避免 sapply() , unlist() + matrix() + sub() 可能是另一种选择

    dt[, EIRange := sub('.*"(\\d+-\\d+)".*', '\\1', matrix(unlist(CutsLabsCV, FALSE, FALSE), ncol = 2L, byrow = TRUE)[, 2L])]
    
    > dt 
          EI                                        CutsLabsCV  EIRange
           <num>                                        <list>  <char>
     1: 101.9163 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     2: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     3: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     4: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     5: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     6: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     7: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     8: 122.0318 ["-Inf",90,130,"Inf"],["<90","90-130",">130"]  90-130
     9: 109.5220 ["-Inf",85,120,"Inf"],["<85","85-120",">120"]  85-120
    10: 109.5220 ["-Inf",85,120,"Inf"],["<85","85-120",">120"]  85-120