在满足dplyr条件的行总数中添加列

时间:2018-07-18 11:07:58

标签: r dplyr

尝试按类别和条件获取总计,但不对数据进行分组。 可复制的示例:

df <- data.frame("class" = c("a","b","c","d","b","b","b","b","c","c","a"),"increment" = c(0,0,0,0,0,0,32,12,0,0,0))

R> df
   class increment
1      a         0
2      b         0
3      c         0
4      d         0
5      b         0
6      b         0
7      b        32
8      b        12
9      c         0
10     c         0
11     a         0

我希望所有情况下增量都不同于零,但对于每个班级。

所需的输出:

R> df
   class increment  increment_count_per_class
1      a         0     0
2      b         0     2
3      c         0     0
4      d         0     0
5      b         0     2
6      b         0     2
7      b        32     2
8      b        12     2
9      c         0     0
10     c         0     0
11     a         0     0

下面是我的第一种方法,但是我知道使用dplyr一定要花些时间:

df <- df %>% mutate(has.increment = ifelse(increment>0,1,0))
R> df
   class increment has.increment
1      a         0             0
2      b         0             0
3      c         0             0
4      d         0             0
5      b         0             0
6      b         0             0
7      b        32             1
8      b        12             1
9      c         0             0
10     c         0             0
11     a         0             0

在存在增量的情况下获取每个班级的总数

N <- df %>% group_by(class,has.increment) %>% tally() %>% filter(has.increment == 1)
R> N
# A tibble: 1 x 3
# Groups:   class [1]
  class has.increment     n
  <chr>         <dbl> <int>
1 b                 1     2

然后加入:

merge(N,df, by = "class", all = TRUE)

R> merge(N,df, by = "class", all = TRUE)
   class has.increment.x  n increment has.increment.y
1      a              NA NA         0               0
2      a              NA NA         0               0
3      b               1  2         0               0
4      b               1  2        12               1
5      b               1  2         0               0
6      b               1  2         0               0
7      b               1  2        32               1
8      c              NA NA         0               0
9      c              NA NA         0               0
10     c              NA NA         0               0
11     d              NA NA         0               0

1 个答案:

答案 0 :(得分:1)

尝试一下:

df %>% 
  group_by(class) %>% 
  mutate(increment_count_per_class = sum(increment!=0))