Reputation: 169
I have a dummy table that gives a 1 if you have the product and 0 if you dont. This table was focused on product, so it has both client and product per line.
However, a client can have multiple products and appear in many lines. I want to group (by sum or count) all the products in one line. Example:
Nulls are supposed to be zeros for the solution answer query to work
And I need this:
Upvotes: 1
Views: 51
Reputation: 2987
An option using dplyr
library(dplyr)
df %>% group_by(Client) %>% summarise_at(vars(TC:CA), function(x) sum(x))
# Client TC CC CA
#1 654 2 1 0
#2 666 0 0 1
#3 667 0 1 0
Data
df <- structure(list(Product = structure(c(3L, 3L, 2L, 1L, 2L), .Label = c("CA",
"CC", "TC"), class = "factor"), Client = structure(c(1L, 1L,
1L, 2L, 3L), .Label = c("654", "666", "667"), class = "factor"),
TC = c(1, 1, 0, 0, 0), CC = c(0, 0, 1, 0, 1), CA = c(0, 0,
0, 1, 0)), .Names = c("Product", "Client", "TC", "CC", "CA"
), row.names = c(NA, -5L), class = "data.frame")
Upvotes: 1