【问题标题】:How to read a list of list in r如何在r中读取列表列表
【发布时间】:2021-03-23 07:39:45
【问题描述】:

我有一个这样的 txt 文件: [["seller_id","product_id","buyer_id","sale_date","quantity","price"],[7,11,49,"2019-01-21",5,3330],[13, 32,6,"2019-02-10",9,1089],[50,47,4,"2019-01-06",1,1343],[1,22,2,"2019-03-03 ",9,7677]]

我想用 R 把它读成这样的表格:

seller_id product_id buyer_id sale_date quantity price
7 11 49 2019-01-21 5 3330
13 32 6 2019-02-10 9 1089
50 47 4 2019-01-06 1 1343
1 22 2 2019-03-03 9 7677

如何编写正确的 R 代码?非常感谢您的宝贵时间。

【问题讨论】:

    标签: r txt


    【解决方案1】:

    更简单的选择是fromJSON

    library(jsonlite)
    library(janitor)
    fromJSON(txt = "file1.txt") %>% 
        as_tibble %>% 
        row_to_names(row_number = 1) %>%
        type.convert(as.is = TRUE)
    

    -输出

    # A tibble: 4 x 6
    #  seller_id product_id buyer_id sale_date  quantity price
    #      <int>      <int>    <int> <chr>         <int> <int>
    #1         7         11       49 2019-01-21        5  3330
    #2        13         32        6 2019-02-10        9  1089
    #3        50         47        4 2019-01-06        1  1343
    #4         1         22        2 2019-03-03        9  7677
    

    【讨论】:

      【解决方案2】:

      您需要将数组中的 json 解析为数据框。也许是这样的:

      # Get string
      str <- '[["seller_id","product_id","buyer_id","sale_date","quantity","price"],[7,11,49,"2019-01-21",5,3330],[13,32,6,"2019-02-10",9,1089],[50,47,4,"2019-01-06",1,1343],[1,22,2,"2019-03-03",9,7677]]'
      
      df_list <- jsonlite::parse_json(str)
      do.call(rbind, lapply(df_list[-1], function(x) {
        setNames(as.data.frame(x), unlist(df_list[1]))}))
      #>   seller_id product_id buyer_id  sale_date quantity price
      #> 1         7         11       49 2019-01-21        5  3330
      #> 2        13         32        6 2019-02-10        9  1089
      #> 3        50         47        4 2019-01-06        1  1343
      #> 4         1         22        2 2019-03-03        9  7677
      

      reprex package (v0.3.0) 于 2020 年 12 月 11 日创建

      【讨论】:

        【解决方案3】:

        一些基本的 R 选项使用:


        • gsub + read.table
        read.table(
          text = gsub('"|\\[|\\]', "", gsub("\\],", "\n", s)),
          sep = ",",
          header = TRUE
        )
        

        • gsub + read.csv
        read.csv(text = gsub('"|\\[|\\]', "", gsub("\\],", "\n", s)))
        

        给了

          seller_id product_id buyer_id  sale_date quantity price
        1         7         11       49 2019-01-21        5  3330
        2        13         32        6 2019-02-10        9  1089
        3        50         47        4 2019-01-06        1  1343
        4         1         22        2 2019-03-03        9  7677
        

        数据

        s <- '[["seller_id","product_id","buyer_id","sale_date","quantity","price"],[7,11,49,"2019-01-21",5,3330],[13,32,6,"2019-02-10",9,1089],[50,47,4,"2019-01-06",1,1343],[1,22,2,"2019-03-03",9,7677]]'
        

        【讨论】:

          猜你喜欢
          • 2012-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-12-22
          • 1970-01-01
          • 2021-11-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多