【问题标题】:Calculate a new dataframe via applying calculations to rows in R通过将计算应用于 R 中的行来计算新的数据框
【发布时间】:2021-08-06 18:35:30
【问题描述】:

我想根据添加某些值来重新处理数据框中的数据,并以相同的方式对所有(数字)列执行此操作。

在代码中,我创建了一个结构有点像这样的数据框:

library(tibble)

df_in <- tribble(~names,        ~a,  ~a_pc,   ~b,   ~b_pc,
                 "Three star",   1L,     1,    2L,      1,
                 "Two star",     5L,     5,   12L,      6,
                 "One star",     6L,     6,  100L,     50,
                 "No star",     88L,    88,   86L,     43,
                 "Empty",        0L,     0,    0L,      0,
                 "Also empty",   0L,     0,    0L,      0)

在我的输出中,我想要一行包含输入数据框中三行的总和,另一行包含其中两行的总和,另一行包含原始行的内容(但已重命名) )。

如果它们有数字,我还想保留其他行,但如果它们为空,则删除它们。我更愿意以编程方式执行此操作,但如果需要,可以使用索引手动执行此操作,所以这不太重要。

我想要的输出有点像这样:


df_out <- tribble(~names,                          ~a,  ~a_pc,   ~b,   ~b_pc,
                  "Any stars",                    12L,    12,  114L,     57,
                  "... of which at least 2 stars", 6L,     6,   14L,      7,
                  "... of which 3 stars",          1L,     1,    2L,      1,
                  "No star",                      88L,    88,   86L,     43)

例如,左上角的 12L(表示 a 列,“任何星星”)是输入的 a 列中 1L5L6L 条目的总和。

我想在处理的这个阶段合并行,因为在我已经计算了百分比列之后执行此操作很重要(在示例中为 ..._pc)。您会在输出中看到百分比列加起来超过 100,这是正确的,因为故意存在一些“重复计算”——如果满足条件,事物可以正确显示在多行中。


编辑添加:请注意,我在测试数据集df_in$names 列中使用的标签不是我在真实情况下拥有的真实标签。我想一个可行的解决方案将能够以某种方式采用指定行集的向量集合和相同数量的字符串的另一个集合来标记新行,并通过它们进行处理。我也许可以像这样定义行集和相关名称:

set_1 <- c("Three star", "Two star", "One star")
set_2 <- c("Three star", "Two star")
set_3 <- "Three star"
set_4 <- "No star"

new_name_1 <- "Any stars"
new_name_2 <- "... of which at least 2 stars"
new_name_3 <- "... of which 3 stars"
new_name_4 <- "No star"

【问题讨论】:

    标签: r row


    【解决方案1】:

    我们可以使用imap 来循环模式(因为某些情况是重叠的)并按sum across 这些列(在filtering 行之后)进行分组

    library(purrr)
    library(stringr)
    
    imap_dfr(setNames(c('(?<!No) star', 'Two|Three', 'Three', 'Empty|No'), 
       c("Any stars", "... of which at least 2 stars", 
          "... of which 3 stars", "No star" )), ~ df_in %>% 
            filter(str_detect(names, regex(.x, ignore_case = TRUE))) %>% 
            group_by(names = .y) %>% 
            summarise(across(everything(), sum)))
    

    -输出

    # A tibble: 4 x 5
      names                             a  a_pc     b  b_pc
      <chr>                         <int> <dbl> <int> <dbl>
    1 Any stars                        12    12   114    57
    2 ... of which at least 2 stars     6     6    14     7
    3 ... of which 3 stars              1     1     2     1
    4 No star                          88    88    86    43
    

    OP 的预期

    > df_out
    # A tibble: 4 x 5
      names                             a  a_pc     b  b_pc
      <chr>                         <int> <dbl> <int> <dbl>
    1 Any stars                        12    12   114    57
    2 ... of which at least 2 stars     6     6    14     7
    3 ... of which 3 stars              1     1     2     1
    4 No star                          88    88    86    43
    

    更新

    如果 OP 正在传递一组自定义名称

    map2_dfr(mget(ls(pattern = '^set_\\d+')),
             mget(ls(pattern = '^new_name_\\d+')), 
             ~ df_in %>%
                       filter(names %in% .x)  %>%
                       group_by(names = .y) %>%
                       summarise(across(everything(), sum)))
                       
    # A tibble: 4 x 5
      names                             a  a_pc     b  b_pc
      <chr>                         <int> <dbl> <int> <dbl>
    1 Any stars                        12    12   114    57
    2 ... of which at least 2 stars     6     6    14     7
    3 ... of which 3 stars              1     1     2     1
    4 No star                          88    88    86    43
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-30
      • 2018-06-18
      • 1970-01-01
      • 2020-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多