Distinct Counting in R

1

I'd like some help to construct a distinct count of a data frame. Here are the data:

Filial  Matrícula   Valor
ABC      100       R$ 500,00
XYZ      200       R$ 850,00
XYZ      100       R$ 320,00
JCT      300       R$ 512,00
JCT      300       R$ 98,00
ABC      300       R$ 1.012,00

I would like R to give me the consolidated showing the distinct count of the "Registration" column as well as the sum of the "Value" column. Similar to what the Excel PivotTable already does. The result I want is:

Filial  Contagem/Matricula      Valor
ABC          2                R$ 1.512,00
JCT          1                R$ 610,00
XYZ          2                R$ 1.170,00
    
asked by anonymous 20.11.2017 / 12:32

2 answers

1

Using the package dplyr

library(dplyr)
dados %>% 
  group_by(Filial) %>% 
  summarise(Contagem = length(unique(Matrícula)), Valor = sum(Valor))
    
21.11.2017 / 16:21
1

Your result example seems to be wrong, there are two lines with Filial equal to JCT . Also, to do accounts we can not use objects of class character , which have R$ , so I only read the numbers without the unit.

agg <- aggregate(Valor ~ Filial, dados, sum)
agg$Contagem <- tapply(dados$Matrícula, dados$Filial, FUN = function(x) length(unique(x)))
agg <- agg[, c(1, 3, 2)]
agg
#  Filial Contagem Valor
#1    ABC        2  1512
#2    JCT        1   610
#3    XYZ        2  1170

DATA.

dados <-
structure(list(Filial = structure(c(1L, 3L, 3L, 2L, 2L, 1L), .Label = c("ABC", 
"JCT", "XYZ"), class = "factor"), Matrícula = c(100L, 200L, 100L, 
300L, 300L, 300L), Valor = c(500, 850, 320, 512, 98, 1012)), .Names = c("Filial", 
"Matrícula", "Valor"), class = "data.frame", row.names = c(NA, 
-6L))
    
20.11.2017 / 14:52