按相似结果分组并检查每组的其他变量中是否有 TRUE/FALSE
Group by similar results and check if TRUE/FALSE among the other variables of each group
我有一个包含不同公司的数据集,这些公司在不同的博客中发表了文章(但他们使用相似的名称,并不总是相同的),我想按相似的结果对它们进行分组,并计算他们发表文章的博客数量.
我想按相似名称结果分组,保留第一个结果的地址,然后检查其余结果的变量中是否有1(已发表文章)或0(未发表文章)结果。
第一部分我有一个类似的问题,但现在我不知道如何同时管理这两个操作。
这是我的数据集的样本:
name address sports_blog nutrition_blog lifestyle_blog nature_blog
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Wellington Adam Martin Sq. 1 1 0 0 0
2 Wellingtoon Adam Martin Sq. 1 0 1 0 0
3 Wellington Co. Adam Martin Sq. 1 0 0 1 0
4 Welinton Adam Martin Sq. 1 0 0 0 1
5 Cornell Blue cross street 1 0 0 0
6 Kornell Blue cross street 0 1 0 0
7 Coornell Blue cross street 0 0 0 1
8 Bleend Aloha avenue 0 0 1 0
9 Blind Aloha avenue 0 0 0 1
10 Laguna River street 1 0 0 0
11 Papito Carnival street 1 0 0 0
12 Papeeto Carnival street 0 0 1 0
因此,我正在寻找这样的东西:
name address sports_blog nutrition_blog lifestyle_blog nature_blog
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Wellington Adam Martin Sq. 1 1 1 1 1
2 Cornell Blue cross street 1 1 0 1
3 Bleend Aloha avenue 0 0 1 1
4 Laguna River street 1 0 0 0
5 Papito Carnival street 1 0 1 0
您只需将其包含在您的分组中即可。使用您之前答案中的函数(由@RuiBarradas 提供),然后
library(dplyr)
df %>%
group_by(name = name[similarGroups(name)], address) %>%
summarise_all(sum)
这给出了,
# A tibble: 5 x 6
# Groups: grp [5]
name address sports_blog nutrition_blog lifestyle_blog nature_blog
<fct> <fct> <int> <int> <int> <int>
1 Bleend Alohaavenue 0 0 1 1
2 Cornell Bluecrossstreet 1 1 0 1
3 Laguna Riverstreet 1 0 0 0
4 Papito Carnivalstreet 1 0 1 0
5 Wellington AdamMartinSq1 1 1 1 1
我有一个包含不同公司的数据集,这些公司在不同的博客中发表了文章(但他们使用相似的名称,并不总是相同的),我想按相似的结果对它们进行分组,并计算他们发表文章的博客数量.
我想按相似名称结果分组,保留第一个结果的地址,然后检查其余结果的变量中是否有1(已发表文章)或0(未发表文章)结果。
第一部分我有一个类似的问题
这是我的数据集的样本:
name address sports_blog nutrition_blog lifestyle_blog nature_blog
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Wellington Adam Martin Sq. 1 1 0 0 0
2 Wellingtoon Adam Martin Sq. 1 0 1 0 0
3 Wellington Co. Adam Martin Sq. 1 0 0 1 0
4 Welinton Adam Martin Sq. 1 0 0 0 1
5 Cornell Blue cross street 1 0 0 0
6 Kornell Blue cross street 0 1 0 0
7 Coornell Blue cross street 0 0 0 1
8 Bleend Aloha avenue 0 0 1 0
9 Blind Aloha avenue 0 0 0 1
10 Laguna River street 1 0 0 0
11 Papito Carnival street 1 0 0 0
12 Papeeto Carnival street 0 0 1 0
因此,我正在寻找这样的东西:
name address sports_blog nutrition_blog lifestyle_blog nature_blog
<chr> <chr> <dbl> <dbl> <dbl> <dbl>
1 Wellington Adam Martin Sq. 1 1 1 1 1
2 Cornell Blue cross street 1 1 0 1
3 Bleend Aloha avenue 0 0 1 1
4 Laguna River street 1 0 0 0
5 Papito Carnival street 1 0 1 0
您只需将其包含在您的分组中即可。使用您之前答案中的函数(由@RuiBarradas 提供),然后
library(dplyr)
df %>%
group_by(name = name[similarGroups(name)], address) %>%
summarise_all(sum)
这给出了,
# A tibble: 5 x 6 # Groups: grp [5] name address sports_blog nutrition_blog lifestyle_blog nature_blog <fct> <fct> <int> <int> <int> <int> 1 Bleend Alohaavenue 0 0 1 1 2 Cornell Bluecrossstreet 1 1 0 1 3 Laguna Riverstreet 1 0 0 0 4 Papito Carnivalstreet 1 0 1 0 5 Wellington AdamMartinSq1 1 1 1 1