Robin
Robin

Reputation: 459

How to read a list of list in r

I have a txt file like this: [["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]]

I would like to read it by R as a table like this:

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

How to write the correct R code? Thanks very much for your time.

Upvotes: 1

Views: 42

Answers (3)

ThomasIsCoding
ThomasIsCoding

Reputation: 101044

Some base R options using:


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

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

which gives

  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

Data

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]]'

Upvotes: 0

Allan Cameron
Allan Cameron

Reputation: 173793

You will need to parse the json from arrays into a data frame. Perhaps something like this:

# 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

Created on 2020-12-11 by the reprex package (v0.3.0)

Upvotes: 2

akrun
akrun

Reputation: 886938

An easier option is fromJSON

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

-output

# 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

Upvotes: 3

Related Questions