简体   繁体   English

在 R 中的嵌套列表中计算

[英]Calculate within nested list in R

How do I calculate the sum of the count per city in nested lists and store it in a new tibble using map?如何计算嵌套列表中每个城市的计数总和并使用地图将其存储在新的 tibble 中?

The output of the nested lists looks like this:嵌套列表的输出如下所示:


City:'s-Hertogenbosch`

$`'s-Hertogenbosch`$`5392`

$`'s-Hertogenbosch`$`5392`$men

 A tibble: 20 x 2

   age              count
   <chr>            <dbl>
 1 0 tot 5 jaar        20
 2 5 tot 10 jaar       40
 3 10 tot 15 jaar      45
 4 15 tot 20 jaar      20
 5 20 tot 25 jaar      25


$`'s-Hertogenbosch`$`5392`$women
 A tibble: 20 x 2
   age              count
   <chr>            <dbl>
 1 0 tot 5 jaar        15
 2 5 tot 10 jaar       30
 3 10 tot 15 jaar      35
 4 15 tot 20 jaar      30
 5 20 tot 25 jaar      15



City:'Aa en Hunze`
$`Aa en Hunze`
$`Aa en Hunze`$`9443`
$`Aa en Hunze`$`9443`$men
 A tibble: 20 x 2
   age              count
   <chr>            <dbl>
 1 0 tot 5 jaar         0
 2 5 tot 10 jaar       10
 3 10 tot 15 jaar       5
 4 15 tot 20 jaar       5
 5 20 tot 25 jaar       5


$`Aa en Hunze`$`9443`$women
 A tibble: 20 x 2
   age              count
   <chr>            <dbl>
 1 0 tot 5 jaar         5
 2 5 tot 10 jaar        5
 3 10 tot 15 jaar       5
 4 15 tot 20 jaar      10
 5 20 tot 25 jaar       5

I know how to store 1 specific postal code in a tibble:我知道如何在小标题中存储 1 个特定的邮政编码:


sum of count <- tibble(sum(data[[1]][[1]][[1]]['count']))

But i can't figure out how to apply this for all lists in R using map:但我不知道如何使用 map 将其应用于 R 中的所有列表:


Get_count_per_postal <- map_int(data, function(x){
    tibble(municipality = names(data), count_inhibitants = sum(data[[?]][[?]][[?]]['count']))
  })

I'm having a hard time accessing the nested lists and apply a calculation, it would be great if someone set me on right path.我很难访问嵌套列表并应用计算,如果有人让我走上正确的道路,那就太好了。

Kr, Quido Kr, 基多

It is not clear clear about the structure correctly.不清楚正确的结构。 Perhaps, a recursive option would work - use rrapply to change the structure by melt ing the nested list , then filter the column 'L4' having only 'count', unnest the list column 'value', do a group_by sum (if we don't want to include 'L3' ie sex as grouping, remove it to get the overall count per 'municipality'也许,递归选项将工作-使用rrapply通过改变结构melt荷兰国际集团嵌套list ,然后filter列“L4”仅具有“计数”, unnestlist栏中的“价值”,做一个group_by sum (如果我们不'不想包含'L3',即sex作为分组,删除它以获得每个'自治市'的总计数

library(rrapply)
library(dplyr)
library(tidyr)
rrapply(data, how = 'melt') %>%
    filter(L4 == 'count') %>% 
    unnest(value) %>%
    group_by(municipality = L1, sex = L3) %>%
    summarise(count_inhabitants = sum(value, na.rm = TRUE), .groups = 'drop')
# A tibble: 4 × 3
  municipality          sex   count_inhabitants
  <chr>                 <chr>             <dbl>
1 City:'Aa en Hunze     men                  25
2 City:'Aa en Hunze     women                30
3 City:'s-Hertogenbosch men                 150
4 City:'s-Hertogenbosch women               125

data数据

data <- list(`City:'s-Hertogenbosch` = list(`'s-Hertogenbosch$5392` = list(
    men = structure(list(age = c("0 tot 5 jaar", "5 tot 10 jaar", 
    "10 tot 15 jaar", "15 tot 20 jaar", "20 tot 25 jaar"), count = c(20, 
    40, 45, 20, 25)), class = c("tbl_df", "tbl", "data.frame"
    ), row.names = c(NA, -5L)), women = structure(list(age = c("0 tot 5 jaar", 
    "5 tot 10 jaar", "10 tot 15 jaar", "15 tot 20 jaar", "20 tot 25 jaar"
    ), count = c(15, 30, 35, 30, 15)), class = c("tbl_df", "tbl", 
    "data.frame"), row.names = c(NA, -5L)))), `City:'Aa en Hunze` = list(
    `'Aa en Hunze$9443` = list(men = structure(list(age = c("0 tot 5 jaar", 
    "5 tot 10 jaar", "10 tot 15 jaar", "15 tot 20 jaar", "20 tot 25 jaar"
    ), count = c(0, 10, 5, 5, 5)), class = c("tbl_df", "tbl", 
    "data.frame"), row.names = c(NA, -5L)), women = structure(list(
        age = c("0 tot 5 jaar", "5 tot 10 jaar", "10 tot 15 jaar", 
        "15 tot 20 jaar", "20 tot 25 jaar"), count = c(5, 5, 
        5, 10, 5)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
    -5L)))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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