【问题标题】:How to automate wrangling excel data with messy data columns and import into R?如何使用杂乱的数据列自动整理excel数据并导入R?
【发布时间】:2021-09-03 09:12:30
【问题描述】:

我正在处理多个 Excel 文件。每个文件都有一组不同格式的工作表。如果所有文件中的每组工作表都相似。我想合并各个工作表,所以我只有一个具有不同工作表的 Excel 文件。每个 Excel 文件都根据区域命名,例如区域A.xlsx,区域B.xlsx。但是,在将各个工作表组合在一起之前,我需要在应用 pivot_longer 和 map_dfr 函数之前将一些工作表重组为整洁的格式。下面的示例显示了输入数据的通用结构:第一列包括年龄组、性别和地区。挑战在于将它们分组到不同的变量中。

pop <- tibble::tibble(
'Age, Sex, Region' = c("Rural", "Total (Females)", "0-4", "5-9", NA, "Urban", "Total (Females)", "0-4", "5-9"),
"2011" = c(NA, 104, 55, 49, NA,NA, 175, 100, 75),
"2012" = c(NA, 108, 57, 51, NA,NA,181, 104, 77))

pop
#> # A tibble: 9 x 3
#>   `Age, Sex, Region` `2011` `2012`
#>   <chr>               <dbl>  <dbl>
#> 1 Rural                  NA     NA
#> 2 Total (Females)       104    108
#> 3 0-4                    55     57
#> 4 5-9                    49     51
#> 5 <NA>                   NA     NA
#> 6 Urban                  NA     NA
#> 7 Total (Females)       175    181
#> 8 0-4                   100    104
#> 9 5-9                    75     77

在应用 pivot_longer 和 map_dfr 函数之前,期望的结果如下所示:

output <- tibble::tibble(
  'age_group' = c("Total (Females)", "0-4", "5-9", "Total (Females)", "0-4", "5-9"),
  'region' = c("Rural", "Rural","Rural","Urban","Urban","Urban"),
  'sex' = c("F","F","F","F","F","F"),
  '2011' = c(104, 55, 49, 175, 100, 75),
  '2012' = c(108, 57, 51, 181, 104, 77),
  'file_id' = c("regionA", "regionA", "regionA", "regionA", "regionA", "regionA"),
  'sheet_id' = c("population", "population", "population", "population", "population", "population"))

output
#> # A tibble: 6 x 7
#>   age_group       region sex   `2011` `2012` file_id sheet_id  
#>   <chr>           <chr>  <chr>  <dbl>  <dbl> <chr>   <chr>     
#> 1 Total (Females) Rural  F        104    108 regionA population
#> 2 0-4             Rural  F         55     57 regionA population
#> 3 5-9             Rural  F         49     51 regionA population
#> 4 Total (Females) Urban  F        175    181 regionA population
#> 5 0-4             Urban  F        100    104 regionA population
#> 6 5-9             Urban  F         75     77 regionA population

其中 regionA 是 Excel 文件的名称,而 population 是工作表的名称

【问题讨论】:

  • 我对 R 还是很陌生,但仍在尝试了解哪个函数做了什么
  • 这听起来你可以用正则表达式解决这个问题。如果模式是严格的:年龄是 X-X,性别在括号中,区域是它应该管理的最后一个。
  • 你能展示你对这个数据框的预期输出吗?你如何识别一个值是否在 age_groupsexregion 中?
  • 如果您的目的是重组您的 Excel 文件,您可能想看看 openxlsx 包,它具有更强大的处理 Excel 文件的功能。您似乎尝试将频率表从 Excel 复制到 R。
  • 为了澄清这个问题,我已经包含了所需的输出。我曾尝试使用 unpivotr,但我无法理解它

标签: r excel


【解决方案1】:

如果我理解正确,我认为这会有所帮助

数据

pop <- 
  tibble::tibble(
  'age_group' = c("Rural", "Total (Females)", "0-4", "5-9", NA, "Urban", "Total (Females)", "0-4", "5-9"),
  "2011" = c(NA, 104, 55, 49, NA,NA, 175, 100, 75),
  "2012" = c(NA, 108, 57, 51, NA,NA,181, 104, 77)
  )

代码

pop %>% 
  filter(!is.na(age_group)) %>% 
  mutate(
    region = if_else(age_group %in% c("Rural","Urban"),age_group,NA_character_),
    sex = case_when(
      str_detect(age_group,"Females") ~ "F",
      str_detect(age_group,"Males") ~ "M",
      TRUE ~ NA_character_
    )
    ) %>% 
  fill(region,sex,.direction = "down") %>% 
  filter(!(age_group %in% c("Rural","Urban"))) %>% 
  select(age_group,region,sex,everything())

输出

  age_group       region sex   `2011` `2012`
  <chr>           <chr>  <chr>  <dbl>  <dbl>
1 Total (Females) Rural  F        104    108
2 0-4             Rural  F         55     57
3 5-9             Rural  F         49     51
4 Total (Females) Urban  F        175    181
5 0-4             Urban  F        100    104
6 5-9             Urban  F         75     77

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多