Building new variables using dplyr

Asked

Viewed 143 times

7

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 build two variables Clientes.Dep and Clientes.Usu using the package dplyr, and those columns would be the sum of everything that involves 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.

Follows a dput 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")

1 answer

5


I managed to resolve, was missing include a . within the function select

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.