Building new variables using dplyr

6

I have the following database

Clientes.Dep..Gratuito.PCG Clientes.Dep..Gratuito Clientes.Dep..Não.Gratuito
                     0                      0                          0
                     0                      0                          0
                    25                      0                          0
                     0                      0                          2
                     0                      0                         79
                     0                      0                         71
Clientes.Usu..Gratuito.PCG Clientes.Usu..Gratuito Clientes.Usu..Não.Gratuito
                    21                      0                          0
                    50                      0                          0
                     0                      0                          0
                    58                      0                          1
                     0                      0                          0
                     0                      0                         16

What I want is to construct two variables Clientes.Dep and Clientes.Usu using the package dplyr , and these columns would be the sum of everything involving Dep. and Usu. , respectively. An example of my attempts is

CLIENTES %>% 
  mutate(Clientes.Dep = rowSums(select(contains("Dep."))),
     Clientes.Usu = rowSums(select(contains("Usu."))))

but without success.

Follow%% of data

structure(list(Clientes.Dep..Gratuito.PCG = c(0, 0, 25, 0, 0, 
0), Clientes.Dep..Gratuito = c(0, 0, 0, 0, 0, 0), Clientes.Dep..Não.Gratuito = c(0, 
0, 0, 2, 79, 71), Clientes.Usu..Gratuito.PCG = c(21, 50, 0, 58, 0, 0),
Clientes.Usu..Gratuito = c(0, 0, 0, 0, 0, 0), Clientes.Usu..Não.Gratuito = c(0, 
0, 0, 1, 0, 16)), .Names = c("Clientes.Dep..Gratuito.PCG", "Clientes.Dep..Gratuito", 
"Clientes.Dep..Não.Gratuito", "Clientes.Usu..Gratuito.PCG", "Clientes.Usu..Gratuito", 
"Clientes.Usu..Não.Gratuito"), row.names = c(NA, 6L), class = "data.frame")
    
asked by anonymous 28.07.2017 / 15:59

1 answer

4

I was able to resolve, I was missing a . inside the function select

CLIENTES %>% 
  mutate(CLIENTES.Dep = rowSums(select(., contains("Dep."))),
         CLIENTES.Usu = rowSums(select(., contains("Usu."))))
    
28.07.2017 / 18:50