Home > Software design >  How to import text data with square brackets as lists in R?
How to import text data with square brackets as lists in R?

Time:12-15

I have a text file that looks like this:

text = '
[2,4,3,1,5]
[6,2,4,3,6]
[[3],[1,5,2]]
[[4],8]
[7]
[[8,9,1]]
'

As you can see the values are lists based on the [ and ] with some nested lists. I was wondering if it is possible to somehow import this text file to get the order of lists mentioned by the square brackets? The desired output should look like this:

list(c(2,4,3,1,5), 
     c(6,2,4,3,6), 
     list(c(3), c(1,5,2)), 
     list(list(4), 8), 
     list(7), 
     list(list(c(8,9,1))))
#> [[1]]
#> [1] 2 4 3 1 5
#> 
#> [[2]]
#> [1] 6 2 4 3 6
#> 
#> [[3]]
#> [[3]][[1]]
#> [1] 3
#> 
#> [[3]][[2]]
#> [1] 1 5 2
#> 
#> 
#> [[4]]
#> [[4]][[1]]
#> [[4]][[1]][[1]]
#> [1] 4
#> 
#> 
#> [[4]][[2]]
#> [1] 8
#> 
#> 
#> [[5]]
#> [[5]][[1]]
#> [1] 7
#> 
#> 
#> [[6]]
#> [[6]][[1]]
#> [[6]][[1]][[1]]
#> [1] 8 9 1

Created on 2022-12-14 with reprex v2.0.2

So I was wondering if it is possible to read the text file above and get a somehow listed structure like the desired output?

CodePudding user response:

We could use reticulate

library(reticulate)
py_run_string(paste("v1=", paste0("[", trimws(gsub("\n",",", text), whitespace = ","), "]")))

-output

py$v1
[[1]]
[1] 2 4 3 1 5

[[2]]
[1] 6 2 4 3 6

[[3]]
[[3]][[1]]
[1] 3

[[3]][[2]]
[1] 1 5 2


[[4]]
[[4]][[1]]
[1] 4

[[4]][[2]]
[1] 8


[[5]]
[1] 7

[[6]]
[[6]][[1]]
[1] 8 9 1

Or another option is

library(jsonlite)
lapply(setdiff(strsplit(text, "\n")[[1]], ""), fromJSON)
  • Related