简体   繁体   中英

Summing occurrences within a list of lists in R

I am new to R.

I have a dataframe that contains variable sized lists. Please see output from dput(head()) at the end for specifics. I need to summarize the data based on the 'name' property. For example, the name "MicroPro #1" occurs 3 times below. I would like to produce a table of all names and the number of times each occurs in the data. Suggestions?


dput(head(temp))
list(structure(list(resource = structure(list(`_class` = c("resource", 
"resource"), oid = c(58742483L, 58744672L), name = c("MicroPro #1", 
"Speedlite Stand"), barcode = c("AAU12902", 
"PH0013204")), class = "data.frame", row.names = 1:2)), class = "data.frame", row.names = 1:2), 
    structure(list(resource = structure(list(`_class` = c("resource", 
    "resource", "resource"), oid = c(1869868L, 24568359L, 41439810L
    ), name = c("Brushes_2", "MicroPro #1", "Ceramics _01"
    ), barcode = c("1910.012", "1149.033", NA)), class = "data.frame", row.names = c(NA, 
    3L))), class = "data.frame", row.names = c(NA, 3L)), structure(list(
        resource = structure(list(`_class` = "resource", oid = 52952320L, 
            name = "MIDI Lab 01", barcode = NA), class = "data.frame", row.names = 1L)), class = "data.frame", row.names = 1L), 
    structure(list(resource = structure(list(`_class` = c("resource", 
    "resource", "resource", "resource"), oid = c(2195740L, 2197584L, 
    1688545L, 61592833L), name = c("Extension Cord", "Soldering Iron", 
    "Scale_Cs", "MicroPro #1"
    ), barcode = c("16.012", "16326.002", "BUSH16312.001", NA
    )), class = "data.frame", row.names = c(NA, 4L))), class = "data.frame", row.names = c(NA, 
    4L)), structure(list(resource = structure(list(`_class` = "resource", 
        oid = 58745398L, name = "Computer Station #03", 
        barcode = NA), class = "data.frame", row.names = 1L)), class = "data.frame", row.names = 1L), 
    structure(list(resource = NA), class = "data.frame", row.names = 1L))
table(unlist(lapply(my_list,\(x)if(is.data.frame(y<-x$resource)) y$name)))

           Brushes_2         Ceramics _01 Computer Station #03       Extension Cord          MicroPro #1 
                   1                    1                    1                    1                    3 
         MIDI Lab 01             Scale_Cs       Soldering Iron      Speedlite Stand 
                   1                    1                    1                    1 

You could also do:

table(subset(stack(unlist(my_list)), grepl('name', ind), values, drop = TRUE))

A bit longer answer, but (IMHO) very readable/adaptable

library(tidyverse)
L %>%
  bind_rows() %>%
  unlist(recursive = FALSE) %>%
  as.data.frame() %>%
  group_by(resource.name) %>%
  summarise(total = n())
# # A tibble: 10 × 2
#   resource.name        total
#   <chr>                <int>
# 1 Brushes_2                1
# 2 Ceramics _01             1
# 3 Computer Station #03     1
# 4 Extension Cord           1
# 5 MicroPro #1              3
# 6 MIDI Lab 01              1
# 7 Scale_Cs                 1
# 8 Soldering Iron           1
# 9 Speedlite Stand          1
#10 NA                       1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM