根据来自 R 中另一个的信息,向数据帧添加一个具有计数总和的新列

时间:2021-07-29 08:25:01

标签: r dataframe dplyr merge datatable

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

这是第一个标签:

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 行中的每个元素(以逗号分隔),以计算 {{1} 中的行数}} where tab2 AND VALUE1 < 10 然后将此计数添加到名为 VALUE2 > 30 的新列中的 tab1 中。

这里是 tab2

Sum_count

示例:

  • 例如对于 tab1 中 row1 的第一个元素: 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 我们在 tab2 (row1) 中看到 VALUE1 < 10 & VALUE2 > 30,所以我数为 1。
  • 对于第二个元素 (row1) :1_G1 我们在 tab2 (row3) 中看到 VALUE1 > 10,所以我数为 0。

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

2_G2

我不知道我说的够不够清楚,不要犹豫,问问题。


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

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

tab2

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))

2 个答案:

答案 0 :(得分:1)

您可以尝试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

答案 1 :(得分: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 日创建