如何读取 r 中的列表列表

How to read a list of list in r

我有一个这样的 txt 文件: [[[seller_id","product_id","buyer_id","sale_date","数量","价格"],[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 作为 table 像这样阅读它:

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代码?非常感谢您的宝贵时间。

更简单的选择是 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

您需要将 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 创建

一些基础 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]]'