【问题标题】:Add a new column with sum of count to a dataframe according to informations from another in R根据来自 R 中另一个的信息,将具有计数总和的新列添加到数据帧
【发布时间】:2021-10-04 21:53:48
【问题描述】:

我需要帮助才能根据另一个 tab2 将计数列添加到名为 tab1 的表中。

这是第一个标签:

tab1

    Event_Groups Other_column
1      1_G1,2_G2            A
2           2_G1            B
3           4_G4            C
4 7_G5,8_G5,9_G5            D

正如您在 Event_Groups 列中看到的那样,我有 2 个信息(EventGroups 数字由“_”分隔)。这些信息也将在tab2$Grouptab2$Event 中找到,其想法是针对tab1(用逗号分隔)中的行中的每个元素,计算tab2 中的行数,其中VALUE1 < 10 AND @ 987654332@,然后将此计数添加到 tab1 中名为 Sum_count 的新列中。

这里是 tab2

  Group Event VALUE1 VALUE2
1    G1     1      5     50  <- VALUE1 < 10 & VALUE2 > 30 : count 1  
2    G1     2     6      20  <- VALUE2 < 30  : count 0 
3    G2     2     50     50  <- VALUE1 > 10  : count 0
4    G3     3      0      0
5    G4     1      0      0
6    G4     4      2     40  <- VALUE1 < 10 & VALUE2 > 30 : count 1 
7    G5     7      1     70  <- VALUE1 < 10 & VALUE2 > 30 : count 1 
8    G5     8      4     67  <- VALUE1 < 10 & VALUE2 > 30 : count 1 
9    G5     9      3     60  <- VALUE1 < 10 & VALUE2 > 30 : count 1 

示例:

  • 例如 tab1 中 row1 的第一个元素:1_G1 我们在 tab2 (row1) 中看到 VALUE1 & VALUE2 > 30,所以我数 1。
  • 对于第二个元素 (row1):2_G2 我们在 tab2 (row3) 中看到 VALUE1 > 10,所以我算 0。

这是预期的结果tab1数据框;

Event_Groups     Other_column Sum_count
1_G1,2_G2        A            1
2_G1             B            0
4_G4             C            1
7_G5,8_G5,9_G5   D            3

我不知道我是否足够清楚,请不要犹豫提出问题。


如果有帮助,这里有两个 dput 格式的表格:

tab1

structure(list(Event_Groups = structure(1:4, .Label = c("1_G1,2_G2", 
"2_G1", "4_G4", "7_G5,8_G5,9_G5"), class = "factor"), Other_column =
structure(1:4, .Label = c("A",  "B", "C", "D"), class = "factor")),
class = "data.frame", row.names = c(NA, 
-4L))

tab2

structure(list(Group = structure(c(1L, 1L, 2L, 3L, 4L, 4L, 5L, 
5L, 5L), .Label = c("G1", "G2", "G3", "G4", "G5"), class = "factor"), 
    Event = c(1L, 2L, 2L, 3L, 1L, 4L, 7L, 8L, 9L), VALUE1 = c(5L, 
    6L, 50L, 0L, 0L, 2L, 1L, 4L, 3L), VALUE2 = c(50, 20, 50, 
    0, 0, 40, 70, 67, 60)), class = "data.frame", row.names = c(NA, 
-9L))

【问题讨论】:

    标签: r dataframe dplyr merge datatable


    【解决方案1】:

    这是一种方法:

    library(dplyr)
    library(tidyr)
    
    tab1 %>% 
      mutate(Event_Groups = as.character(Event_Groups)) %>% 
      separate_rows(Event_Groups, sep = ",") %>% 
      left_join(., 
                tab2 %>% 
                  unite(col = "Event_Groups", Event, Group) %>% 
                  mutate(count = if_else(VALUE1 < 10 & VALUE2 > 30,1L, 0L))) %>%
      group_by(Other_column) %>% 
      summarise(Event_Groups = paste(unique(Event_Groups), collapse = ","),
                Sum_count = sum(count)) %>% 
      select(Event_Groups, everything())
    
    #> Joining, by = "Event_Groups"
    #> `summarise()` ungrouping output (override with `.groups` argument)
    #> # A tibble: 4 x 3
    #>   Event_Groups   Other_column Sum_count
    #>   <chr>          <fct>            <int>
    #> 1 1_G1,2_G2      A                    1
    #> 2 2_G1           B                    0
    #> 3 4_G4           C                    1
    #> 4 7_G5,8_G5,9_G5 D                    3
    

    reprex package (v0.3.0) 于 2021 年 7 月 29 日创建

    【讨论】:

    • 非常感谢您!如果我有多个 Other_column 需要保留在输出中但没有特别参与该过程怎么办?
    • 您可以尝试将它们全部添加到我们现在只有Other_columngroup_by 吗?如果您有许多其他变量,您可以在group_by 中使用across
    • 是的,当然,只是另一个问题:你为什么将1L, 0L 放入 mutate 行?
    • 0L1Lifelse 的一部分,但您可以使用 as.numeric 而不是 ifelse 语句,那么您就不需要 0L1L .
    【解决方案2】:

    你可以试试tidyverse

    library(tidyverse)
    
    tab1 %>% 
      rownames_to_column() %>% 
      separate_rows(Event_Groups, sep = ",") %>% 
      separate(Event_Groups, into =  c("Event", "Group"), sep="_", convert = T) %>% 
      left_join(tab2 %>% 
                 mutate(count = as.numeric(VALUE1 < 10 & VALUE2 > 30)), 
                by = c("Event", "Group")) %>% 
      unite(Event_Groups, Event, Group) %>% 
      group_by(rowname)  %>% 
      summarise(Event_Groups = toString(Event_Groups),
                Other_column = unique(Other_column),
                count =sum(count))
    # A tibble: 4 x 4
      rowname Event_Groups     Other_column count
      <chr>   <chr>            <chr>        <dbl>
    1 1       1_G1, 2_G2       A                1
    2 2       2_G1             B                0
    3 3       4_G4             C                1
    4 4       7_G5, 8_G5, 9_G5 D                3
    

    【讨论】:

    • 非常感谢您的帮助,如果我有多个 Other_column 需要保留在输出中但没有特别参与该过程怎么办?
    猜你喜欢
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2022-01-09
    • 2021-03-22
    相关资源
    最近更新 更多