0

Hi all my dataframe looks somewhat like this:

**| Descriptor  |**
[{"name": "Some name", "id": "L73871287"}, {"name": "Another name", "id": "L7123287"}]
[{"name": "Yet another name", "id": "L73556287"}, {"name": "Yet another name", "id": "L73556287"}]

How would one go about splitting this data by objects in R? So to get:

**| Descriptor  |**
{"name": "Some name", "id": "L73871287"}
{"name": "Another name", "id": "L7123287"}
{"name": "Yet another name", "id": "L73556287"}
{"name": "Yet another name", "id": "L73556287"}

Even better would be to just get a column "name" and a column "id", but idk if this is possible in R (I have a python and javascript background, but the file was too large for python)

Shandorius
  • 13
  • 1
  • 5
  • 1
    This strongly looks like JSON data, you may want to look into [jsonlite](https://cran.r-project.org/web/packages/jsonlite/index.html). Please consider this before posting: https://stackoverflow.com/a/5963610/6574038 – jay.sf Feb 21 '21 at 11:06

1 Answers1

1

Maybe this is what you are looking for:

library(jsonlite)

json <- '[{"name": "Some name", "id": "L73871287"}, {"name": "Another name", "id": "L7123287"}],
[{"name": "Yet another name", "id": "L73556287"}, {"name": "Yet another name", "id": "L73556287"}]'

ls <- fromJSON(txt = paste0("[", json, "]"))

do.call(rbind, ls)
#>               name        id
#> 1        Some name L73871287
#> 2     Another name  L7123287
#> 3 Yet another name L73556287
#> 4 Yet another name L73556287
stefan
  • 90,330
  • 6
  • 25
  • 51
  • 1
    I suggest using `do.call(rbind, ls)` which is more stable over time. – jay.sf Feb 21 '21 at 11:14
  • 1
    Thanks @jay.sf . Sigh. That's the problem if one started learning R with the Tidyverse. One lacks all the base R functionalities. Therefore I'm always glad for any suggestions on how to do things using base R. Best S. – stefan Feb 21 '21 at 11:24
  • 1
    Welcome! It's documented [here](https://cran.r-project.org/doc/manuals/r-release/R-lang.html#index-do_002ecall), find all CRAN manuals [there](https://cran.r-project.org/manuals.html). – jay.sf Feb 21 '21 at 11:32
  • 1
    Thanks @jay.sf for sharing the links. Now it looks I have some new bed time readings for the next few months or so. :D – stefan Feb 21 '21 at 11:54
  • Yeah, have fun! :) – jay.sf Feb 23 '21 at 11:05