簡體   English   中英

在 R 中的嵌套列表中計算

[英]Calculate within nested list in R

如何計算嵌套列表中每個城市的計數總和並使用地圖將其存儲在新的 tibble 中?

嵌套列表的輸出如下所示:


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

我知道如何在小標題中存儲 1 個特定的郵政編碼:


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

但我不知道如何使用 map 將其應用於 R 中的所有列表:


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

我很難訪問嵌套列表並應用計算,如果有人讓我走上正確的道路,那就太好了。

Kr, 基多

不清楚正確的結構。 也許,遞歸選項將工作-使用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 <- 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