R语言 如何根据过滤条件添加计数列而不是在 dplyr 中分组?



下面是我的资产数据的子集

# df1 <- AllAssets %>%
#   filter(country %in% c('Morocco', 'Gabon', 'Tunisia')) %>%
#   group_by(country, named, active) %>%
#   summarize(assets = n())

此处等效于此数据帧:

library(dplyr)
library(tibble)
df1 <- structure(list(country = c("Gabon", "Gabon", "Gabon", "Morocco", 
"Morocco", "Tunisia", "Tunisia", "Tunisia"), named = c(FALSE, 
TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE), active = c(1, 0, 
1, 0, 1, 0, 0, 1), assets = c(8L, 305L, 271L, 254L, 18L, 24L, 
350L, 282L)), class = "data.frame", row.names = c(NA, -8L), .Names = c("country", 
"named", "active", "assets")) %>% as.tibble() %>% group_by(country, named)
# A tibble: 8 x 4
# Groups:   country, named [5]
# country named active assets
# <chr>   <lgl>  <dbl>  <int>
# 1 Gabon   FALSE     1.      8
# 2 Gabon   TRUE      0.    305
# 3 Gabon   TRUE      1.    271
# 4 Morocco TRUE      0.    254
# 5 Morocco TRUE      1.     18
# 6 Tunisia FALSE     0.     24
# 7 Tunisia TRUE      0.    350
# 8 Tunisia TRUE      1.    282

我正在制作一个电子表格,根据不同的可变条件计算一个国家的资产#。有没有比我在下面写的更简单、更干净的方法来获得我所做的输出?

df1 %>%
mutate(ctry_namedTF_count = sum(assets)) %>% 
group_by(country) %>% 
mutate(ctry_count = sum(assets)) %>% 
filter(named == TRUE, active == 1) %>% 
select(-(named:active)) %>% 
rename(named_active = assets, 
TotalAssets = ctry_count,
named = ctry_namedTF_count)
# Output:
# A tibble: 3 x 4
# Groups:   country [3]
country named_active named TotalAssets
<chr>          <int> <int>       <int>
1 Gabon            271   576         584
2 Morocco           18   272         272
3 Tunisia          282   632         656

我本质上是按照 dplyr 小插图(Ctrl-F "汇总")中的描述"汇总"我的数据帧,反复调用 sum(),并重复添加一个数字,而不仅仅是计算分组案例。但是我所拥有的虽然功能强大,但很难阅读,我想知道是否有更简单的方法或自定义功能更有意义。

例如,dplyr::add_count 用于添加对一列或多列中的组事例进行计数的列非常简单,

> df1 %>% add_count(country, named)
# A tibble: 8 x 5
# Groups:   country, named [5]
country named active assets     n
<chr>   <lgl>  <dbl>  <int> <int>
1 Gabon   FALSE     1.      8     1
2 Gabon   TRUE      0.    305     2
3 Gabon   TRUE      1.    271     2
4 Morocco TRUE      0.    254     2
5 Morocco TRUE      1.     18     2
6 Tunisia FALSE     0.     24     1
7 Tunisia TRUE      0.    350     2
8 Tunisia TRUE      1.    282     2

我想知道是否有东西可以在这些分组中对变量求和。

在基本 R 或其他修改包中是否存在任何这样的函数? 像df1 %>% add_aggregate_by_vars_filters(vars = named, filter = 'named == TRUE', sum_var = assets)这样的东西,还是类似干净实用的东西?

library(dplyr)
df1 <- structure(list(country = c("Gabon", "Gabon", "Gabon", "Morocco", 
"Morocco", "Tunisia", "Tunisia", "Tunisia"), named = c(FALSE, 
TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE), active = c(1, 0, 
1, 0, 1, 0, 0, 1), assets = c(8L, 305L, 271L, 254L, 18L, 24L, 
350L, 282L)), class = "data.frame", row.names = c(NA, -8L), .Names = c("country", 
"named", "active", "assets"))
df1 %>%
group_by(country) %>%
summarise(named_active = sum(assets[named==TRUE & active==1]),
named = sum(assets[named==TRUE]),
TotalAssets = sum(assets[active==1]))
# # A tibble: 3 x 4
#   country named_active named TotalAssets
#   <chr>          <int> <int>       <int>
# 1 Gabon            271   576         279
# 2 Morocco           18   272          18
# 3 Tunisia          282   632         282

最新更新