0

Using R, I'm trying to summarize and count occurrences of a dataframe whose contents is other dataframes. It's multi-dimensional, in other words, and the number of items varies for each element in the dataframe. I'm not very experienced, so I hope I write this correctly:

data is at:

<https://pastebin.com/1f5bAi2s>

I'd like to count the times each resource occurs in the data, for example "Xbox One, NM2" or "PS4, NM2"

Thank in advance for the help.

  • 2
    can you provide some sample data? your problem is quite hard to reproduce this way. `dput()` can help you to provide some sample data – VvdL Jun 03 '22 at 14:35
  • 1
    this is not r code – Onyambu Jun 03 '22 at 14:37
  • 1
    (1) *"count occurrences of a dataframe"* ... what about this dataframe are you counting? Rows? Occurrence of a particular value in one of its simple columns? Occurrence of a particular value in one of its nested frames? (2) The lack of representative R data is making it rather difficult to interpret what you have, what you mean, and what you need. Please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info for discussions on reproducible questions; notably, the use of `dput(head(x))` might be a good start ([edit] and paste into a code block). – r2evans Jun 03 '22 at 14:38
  • Welcome to SO, EricDodson! Asking questions *well* is contextual here on SO and perhaps particularly in the [tag:r] tag, those links should provide a good start: please, really, read through them and see how providing representative data can greatly help you to get an answer quickly. – r2evans Jun 03 '22 at 14:40
  • Thanks for the suggestions, and sorry for the lack of clarity. I don't exactly know how to get the data request. dput() has really long output, hundreds of lines or more, even head(dput() is hundreds of lines. – Eric Dodson Jun 03 '22 at 14:55
  • try dput(head(your_data)) – VvdL Jun 03 '22 at 15:01
  • I don't think you need more than item$name to create some example data. Maybe you can include this in your question so people can help you better: `item_1 <- data.frame(name = rep("random", 3)) item_2 <- data.frame(name = rep("random", 6)) item_3 <- data.frame(name = rep("random", 7)) item_4 <- data.frame(name = rep("random", 13)) item_5 <- data.frame(name = rep("random", 2)) item_6 <- data.frame(name = rep("random", 6)) list_1 <- list(item_1 = item_1, item_2 = item_2) list_2 <- list(item_3 = item_3, item_4 = item_4, item_5 = item_5, item_6 = item_6) p <- c(list_1, list_2)` – VvdL Jun 03 '22 at 15:30
  • The output from dput(head(your_data)) is 118 lines long, is that too much to be helpful? – Eric Dodson Jun 03 '22 at 16:22
  • Do we need ***all*** columns of those 6 rows to be able to reproduce the issue, or will just 10 (for example) columns suffice? Perhaps just `dput(x[1:6,1:10])`? We don't need *everything*, but we need *something*. It doesn't have to be real data, either: if you can create sample data with `data.frame(..)`, that's also good enough. – r2evans Jun 03 '22 at 16:38
  • All the data is JSON. I'm sorry if this is considered spamming: – Eric Dodson Jun 03 '22 at 19:08
  • Actually the dput output is way too long. Here is an attempt to shorten it: – Eric Dodson Jun 03 '22 at 19:11
  • See edited question for link to Pastebin – Eric Dodson Jun 03 '22 at 19:28

1 Answers1

0

It's not clear what you want from this nested list. Here's the structure returned by str:

str(dat)
'data.frame':   6 obs. of  9 variables:
 $ _class                   : chr  "allocation" "allocation" "allocation" "allocation" ...
 $ name                     : chr  "CK-76303" "CK-77297" "CK-77432" "CK-70045" ...
 $ invoices                 :List of 6
  ..$ :'data.frame':    0 obs. of  0 variables
  ..$ :'data.frame':    0 obs. of  0 variables
  ..$ :'data.frame':    0 obs. of  0 variables
  ..$ :'data.frame':    0 obs. of  0 variables
  ..$ :'data.frame':    0 obs. of  0 variables
  ..$ :'data.frame':    0 obs. of  0 variables
 $ items                    :List of 6
  ..$ :'data.frame':    1 obs. of  1 variable:
  .. ..$ resource:'data.frame': 1 obs. of  4 variables:
  .. .. ..$ _class : chr "resource"
  .. .. ..$ oid    : int 58739185
  .. .. ..$ name   : chr "Studio 1"
  .. .. ..$ barcode: logi NA
  ..$ :'data.frame':    1 obs. of  1 variable:
  .. ..$ resource:'data.frame': 1 obs. of  4 variables:
  .. .. ..$ _class : chr "resource"
  .. .. ..$ oid    : int 58739172
  .. .. ..$ name   : chr "Studio 1"
  .. .. ..$ barcode: logi NA
  ..$ :'data.frame':    1 obs. of  1 variable:
  .. ..$ resource:'data.frame': 1 obs. of  4 variables:
  .. .. ..$ _class : chr "resource"
  .. .. ..$ oid    : int 1872017
  .. .. ..$ name   : chr "Drill Bit "
  .. .. ..$ barcode: chr "1721.019"
  ..$ :'data.frame':    9 obs. of  1 variable:
  .. ..$ resource: logi  NA NA NA NA NA NA ...
  ..$ :'data.frame':    19 obs. of  1 variable:
  .. ..$ resource:'data.frame': 19 obs. of  4 variables:
  .. .. ..$ _class : chr  "resource" "resource" "resource" "resource" ...
  .. .. ..$ oid    : int  61437789 61437748 61437699 61437651 58694528 58694527 58694526 58694525 58694524 58694523 ...
  .. .. ..$ name   : chr  "Xbox One, NM2 " "Xbox One, NM2 " "PS4, NM2 " "PS4, NM2 " ...
  .. .. ..$ barcode: logi  NA NA NA NA NA NA ...
  ..$ :'data.frame':    1 obs. of  1 variable:
  .. ..$ resource:'data.frame': 1 obs. of  4 variables:
  .. .. ..$ _class : chr "resource"
  .. .. ..$ oid    : int 41390788
  .. .. ..$ name   : chr "Model Shop"
  .. .. ..$ barcode: chr "IND2.1.004"
 $ allocationContentsSummary:'data.frame':  6 obs. of  2 variables:
  ..$ groups:List of 6
  .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. ..$ label: chr "Reservation Canceled"
  .. .. ..$ items:List of 1
  .. .. .. ..$ :'data.frame':   1 obs. of  2 variables:
  .. .. .. .. ..$ label   : chr " Studios 20"
  .. .. .. .. ..$ subitems:List of 1
  .. .. .. .. .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. .. .. .. .. ..$ label: chr "Studio 1"
  .. .. .. .. .. .. ..$ due  : logi NA
  .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. ..$ label: chr "Reservation Canceled"
  .. .. ..$ items:List of 1
  .. .. .. ..$ :'data.frame':   1 obs. of  2 variables:
  .. .. .. .. ..$ label   : chr "Studios 20"
  .. .. .. .. ..$ subitems:List of 1
  .. .. .. .. .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. .. .. .. .. ..$ label: chr "Studio 1"
  .. .. .. .. .. .. ..$ due  : logi NA
  .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. ..$ label: chr "Returned"
  .. .. ..$ items:List of 1
  .. .. .. ..$ :'data.frame':   1 obs. of  2 variables:
  .. .. .. .. ..$ label   : chr "Drill Bits "
  .. .. .. .. ..$ subitems:List of 1
  .. .. .. .. .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. .. .. .. .. ..$ label: chr "Drill Bit_High "
  .. .. .. .. .. .. ..$ due  : logi NA
  .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. ..$ label: chr "Reservation Canceled"
  .. .. ..$ items:List of 1
  .. .. .. ..$ :'data.frame':   2 obs. of  2 variables:
  .. .. .. .. ..$ label   : chr  "Music Classrom 110" "Music Studio 113 Vocal Tracking"
  .. .. .. .. ..$ subitems:List of 2
  .. .. .. .. .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. .. .. .. .. ..$ label: chr "8 by type"
  .. .. .. .. .. .. ..$ due  : logi NA
  .. .. .. .. .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. .. .. .. .. ..$ label: chr "1 by type"
  .. .. .. .. .. .. ..$ due  : logi NA
  .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. ..$ label: chr "Reservation Canceled"
  .. .. ..$ items:List of 1
  .. .. .. ..$ :'data.frame':   1 obs. of  2 variables:
  .. .. .. .. ..$ label   : chr "eSpiLounge"
  .. .. .. .. ..$ subitems:List of 1
  .. .. .. .. .. ..$ :'data.frame': 19 obs. of  2 variables:
  .. .. .. .. .. .. ..$ label: chr  "Nintendo, NM2 " "Nintendo, NM2 " "PC-01GAM" "PC-02GAM" ...
  .. .. .. .. .. .. ..$ due  : logi  NA NA NA NA NA NA ...
  .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. ..$ label: chr "Reservation Canceled"
  .. .. ..$ items:List of 1
  .. .. .. ..$ :'data.frame':   1 obs. of  2 variables:
  .. .. .. .. ..$ label   : chr "ModelS "
  .. .. .. .. ..$ subitems:List of 1
  .. .. .. .. .. ..$ :'data.frame': 1 obs. of  2 variables:
  .. .. .. .. .. .. ..$ label: chr "ModelS_04"
  .. .. .. .. .. .. ..$ due  : logi NA
  ..$ lock  : logi  NA NA NA NA NA NA
 $ itemCount                : int  1 1 1 9 19 1
 $ patronAcademicLevel      : chr  NA "BFA/BFA-PH" "BFA/BFA-JEM" "10" ...
 $ realEndTime              : chr  NA NA "2022-05-11T12:21:00.000000-07:00" NA ...
 $ realStartTime            : chr  NA NA "2022-05-11T08:58:00.000000-07:00" NA ...

Perhaps you want the items in the "items" node: Within each of those 6 dataframes there are character values that are each in the [[1]]$name leaves of those enclosing dataframe structures:

sapply(dat$items, function(x) x[[1]]['name'])
$name
[1] "Studio 1"

$name
[1] "Studio 1"

$name
[1] "Drill Bit "

[[4]]
[1] NA

$name
 [1] "Xbox One, NM2 " "Xbox One, NM2 " "PS4, NM2 "      "PS4, NM2 "      "PC, NM2"        "PC, NM2"       
 [7] "PC, NM2"        "PC, NM2-"       "PC, NM2"        "Nintendo, NM2 " "Nintendo, NM2 "

$name
[1] "Model Shop"
IRTFM
  • 258,963
  • 21
  • 364
  • 487