0

I think this one is simple but I still couldn't figure it out and I'm kind in a hurry with it.

Here's the deal:

I'm importing json files into R, and then I'd like to subset them.

Lets say I have this list called files

"files"

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

and inside these files I have other lists, which are the same for the whole "files"

"files" (list 1 expanded)

    [[1]]
        >[response]
        >[responseHeader]
    [[2]]
    [[3]]
    [[4]]

and so on.

The thing is: I'd like to create a new list containing subsets from the files lists.

For example, let's say that the files[[1]]$reponse$title has the title of the document. I'd like to extract that title to that new df, so it would look like this:

files

[[title1]]
[[title2]]
[[title2]]
[[title4]]

I've tried sapply, lapply and everything but I couldn't figure out what's wrong.

EDIT: here's an print of the Rstudio Viewer from FILES

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Lucca Ramalho
  • 573
  • 1
  • 6
  • 16
  • 2
    a small example would help, but perhaps just `lapply(files, function(x) x$reponse$title)` - or is that what you tried?? – user20650 Apr 24 '18 at 18:43
  • i've tried that but it returns `[[1]][[NULL]]` – Lucca Ramalho Apr 24 '18 at 18:48
  • 1
    so perhaps there is no `files[[1]]$reponse$title`? Really need to see a **small** example of files – user20650 Apr 24 '18 at 18:50
  • the ideia is that i need the title as the name of the whole list with lots of others information inside.. i hope i've made myself clear! The 1 problems is how to subset the lists for the whole document and the second one is how to use the title name inside that list to be the name of it =) – Lucca Ramalho Apr 24 '18 at 18:50
  • actually there is! For example, when i type `files[[1]]$response$docs` it returns me a list of the documentation... So let's say i want to create a new list with it it should return me something like; `[doc1]` `[doc2]` ... and so on – Lucca Ramalho Apr 24 '18 at 18:53
  • i've edited the post and added the image of the list, hope you can figure it out https://i.stack.imgur.com/KEYd1.png – Lucca Ramalho Apr 24 '18 at 18:58
  • It is inordinately hard to guess the solution to your problem based on pictures and pseudocode, which is why it's preferable to have a reproducible example (not necessarily your original problem; you could make up data to illustrate). Some guidance https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/28481250#28481250 – Frank Apr 24 '18 at 19:02
  • oh gosh, sorry about that... should i close this one and open up a new one? i'm kind in a hurry... i fear that if i edit this one it will "flop" – Lucca Ramalho Apr 24 '18 at 19:06
  • I'm not used to looking like this in Rstudio, but is the structure like `files > 4 sublists > 3 sublists (responseHeader, response, docs)`, and each of these final lists have further sublists? I think to extract *docs* it should be enough to do `lapply(files, "[[", "docs")` or `lapply(files, function(x) x$docs)` - its easier to iterate down the list with the second approach – user20650 Apr 24 '18 at 19:08
  • it worked to reach until "docs", but nothing further =( it returns null when i try to select another one... guess i'll submit another question – Lucca Ramalho Apr 24 '18 at 19:23
  • from your image it seems as if docs is a data.frame. I'd guess that any further extraction would grab columns from docs eg `lapply(files, function(x) x$docs[[1]])` to grab the first column (you could use `x$docs$varname` if you know `varname`) – user20650 Apr 24 '18 at 19:38
  • But yes, if you submit a new question please include an example of the data - you can create your own so that it is representative of your actual data. It makes it a lot easier for you to get help. – user20650 Apr 24 '18 at 19:39
  • @user20650 thanks a lot! It worked for me =))))) Actually, before you answered me i've done this way: `files <- lapply(files, "[[", "response")` and then `files <- lapply(files, '[[", "docs")` because somehow i couldn't do straight like: `files <- lapply(files "[[" "response$docs")` – Lucca Ramalho Apr 24 '18 at 19:41
  • great, i'll keep that in mind. Thanks a lot! – Lucca Ramalho Apr 24 '18 at 19:45

0 Answers0