按列分组并计算与R中的每个值相对应的正值和负值的数量

时间:2019-02-20 11:34:07

标签: r count grouping

我希望有一个正值和负值的列表,这些值与对列进行分组后出现的每个值相对应。我的数据如下:

dataset <- read.table(text = 
"id value
1 4
1 -2
1 0
2 6
2 -4
2 -5
2 -1
3 0
3 0
3 -4
3 -5",
header = TRUE, stringsAsFactors = FALSE)

我希望我的结果看起来像这样:

id num_pos_value num_neg_value num_zero_value
1 1 1 1 
2 1 3 0
3 0 2 2

我想通过添加正值和负值的总和来扩展上述结果的列。

id num_pos num_neg num_zero sum_pos sum_neg
1 1 1 1 4 -2
2 1 3 0 6 -10
3 0 2 2 0 -9

1 个答案:

答案 0 :(得分:0)

我们通过'id'创建一个分组,并计算逻辑向量的sum

library(dplyr)
df1 %>% 
   group_by(id) %>% 
   summarise(num_pos = sum(value > 0), 
             num_neg = sum(value < 0), 
             num_zero = sum(value == 0))
# A tibble: 3 x 4
#     id num_pos num_neg num_zero
#  <int>   <int>   <int>    <int>
#1     1       1       1        1
#2     2       1       3        0
#3     3       0       2        2

或者获取tablesign的“值”,并将spread设为“宽”

library(tidyr)
df1 %>% 
   group_by(id) %>% 
   summarise(num = list(table(factor(sign(value), levels = -1:1)))) %>%
   unnest %>% 
   mutate(grp = rep(paste0("num", c("pos", "zero", "neg")), 3)) %>% 
   spread(grp, num)

或使用count

df1 %>%
   count(id, val = sign(value)) %>% 
   spread(val, n, fill = 0)

数据

df1 <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 
 3L), value = c(4L, -2L, 0L, 6L, -4L, -5L, -1L, 0L, 0L, -4L, -5L
 )), class = "data.frame", row.names = c(NA, -11L))