2

该示例显示了不同工厂的生产输出的测量值,其中第一列表示工厂,最后一列表示生产量。

factory <- c("A","A","B","B","B","B","B","C","D")
production <- c(15, 2, 1, 1, 2, 1, 2,20,5)
df <- data.frame(factory, production)
df
  factory production
1       A         15
2       A          2
3       B          1
4       B          1
5       B          2
6       B          1
7       B          2
8       C         20
9       D          5

现在我想根据这个数据集中的总产量将工厂归为更少的级别。

使用普通的 forcats::fct_lump,我可以按你出现的行数来汇总它们,例如制作 3 个级别:

library(tidyverse)    
df %>% mutate(factory=fct_lump(factory,2))
      factory production
    1       A         15
    2       A          2
    3       B          1
    4       B          1
    5       B          2
    6       B          1
    7       B          2
    8   Other         20
    9   Other          5

但我想根据总和(生产)对它们进行汇总,保留前 n = 2 个工厂(按总产量)并将其余工厂汇总。期望的结果:

1       A         15
2       A          2
3   Other          1
4   Other          1
5   Other          2
6   Other          1
7   Other          2
8       C         20
9   Other          5

有什么建议么?

谢谢!

4

3 回答 3

4

这里的关键是应用特定的理念,以便根据生产总和将工厂组合在一起。请注意,这种理念与您在(真实)数据集中拥有的实际值有关。

选项1

这是一个将总产量等于或小于 15 的工厂组合在一起的示例。如果您想要另一个分组,您可以修改阈值(例如使用 18 而不是 15)

factory <- c("A","A","B","B","B","B","B","C","D")
production <- c(15, 2, 1, 1, 2, 1, 2,20,5)
df <- data.frame(factory, production, stringsAsFactors = F)

library(dplyr)

df %>%
  group_by(factory) %>%
  mutate(factory_new = ifelse(sum(production) > 15, factory, "Other")) %>%
  ungroup()

# # A tibble: 9 x 3
#   factory production factory_new
#   <chr>        <dbl> <chr>      
# 1 A               15 A          
# 2 A                2 A          
# 3 B                1 Other      
# 4 B                1 Other      
# 5 B                2 Other      
# 6 B                1 Other      
# 7 B                2 Other      
# 8 C               20 C          
# 9 D                5 Other 

我正在创建factory_new而不删除(原始)factory列。

选项 2

这是一个示例,您可以根据工厂的产量对工厂进行排名/排序,然后您可以选择一些顶级工厂保持原样并将其余工厂分组

factory <- c("A","A","B","B","B","B","B","C","D")
production <- c(15, 2, 1, 1, 2, 1, 2,20,5)
df <- data.frame(factory, production, stringsAsFactors = F)

library(dplyr)

# get ranked factories based on sum production
df %>%
  group_by(factory) %>%
  summarise(SumProd = sum(production)) %>%
  arrange(desc(SumProd)) %>%
  pull(factory) -> vec_top_factories

# input how many top factories you want to keep
# rest will be grouped together
n = 2

# apply the grouping based on n provided
df %>%
  group_by(factory) %>%
  mutate(factory_new = ifelse(factory %in% vec_top_factories[1:n], factory, "Other")) %>%
  ungroup()

# # A tibble: 9 x 3
#   factory production factory_new
#   <chr>        <dbl> <chr>      
# 1 A               15 A          
# 2 A                2 A          
# 3 B                1 Other      
# 4 B                1 Other      
# 5 B                2 Other      
# 6 B                1 Other      
# 7 B                2 Other      
# 8 C               20 C          
# 9 D                5 Other 
于 2018-10-04T14:58:14.043 回答
4

只需指定权重参数w

> df %>% 
+   mutate(factory = fct_lump_n(factory, 2, w = production))
  factory production
1       A         15
2       A          2
3   Other          1
4   Other          1
5   Other          2
6   Other          1
7   Other          2
8       C         20
9   Other          5

注意:使用forcats::fct_lump_n因为fct_lump不再推荐泛型。

于 2021-02-24T19:46:54.097 回答
1

我们也可以base R通过创建一个逻辑条件来使用ave

df$factory_new <- "Other"
i1 <- with(df, ave(production, factory, FUN = sum) > 15)
df$factory_new[i1] <- df$factory[i1]
于 2018-10-04T15:15:29.700 回答