Calculate number of tickets by client

时间:2018-03-25 19:08:12

标签: r dplyr

I'm a beginner with R.

I have a df with 3 columns : IDTICKET, IDCLIENT and TIC_TOTAL_TCC

I want to calculate the number of tickets that have TIC_TOTAL_TCC < 0 by client.

I begin with something like that:

nb_ticket_remb <- merge_all2 %>%
  group_by(IDCLIENT,IDTICKET) %>%
  summarise(RemboursementTicket = sum(TIC_TOTALTTC[TIC_TOTALTTC] < 0))

But it's not the good result. How can I do this with dplyr? Thanks a lot for your help.

2 个答案:

答案 0 :(得分:1)

I'm not sure that I understood your question, but this might help:

IDCLIENT <- rep(sample(1:5), 3)
IDTICKET <- rep(sample(1:5), 3)
TIC_TOTAL_TCC <- rep(sample(1:5), 3)
library(dplyr)
tibble(IDCLIENT, IDTICKET, TIC_TOTAL_TCC) %>%
  group_by(IDCLIENT) %>%
  filter(TIC_TOTAL_TCC < 0) %>%
  summarise(count = n())

The function n, used above returns the number of rows of the original data set that have TIC_TOTAL_TCC < 0 for each value of IDCLIENT. Is that what you seek?

答案 1 :(得分:0)

我遇到了一些困难,因为你的一个错误是我无法识别的拼写错误:export default combineReducers({ article, articleList, auth, router: routerReducer }); 。如果您先过滤,可以使用TIC_TOTAL_TCC != TIC_TOTAL_TTC获取结果:

n()

一旦我纠正了拼写错误(以及索引值本身的错误),您的代码也会起作用:

set.seed(123)
IDCLIENT <- sample(1:5, 33,repl=TRUE)
IDTICKET <- sample(1:5, 33,repl=TRUE)
TIC_TOTAL_TCC <- sample( (-2):5, 33, repl=TRUE)
library(dplyr)
merge_all2 <- tibble(IDCLIENT, IDTICKET, TIC_TOTAL_TCC)
merge_all2 %>% filter( TIC_TOTAL_TCC < 0) %>%  
               group_by(IDCLIENT) %>% 
                summarise( n=n() )