aggregate(Quantity ~ Type + Mode + Company, df, sum)
# Type Mode Company Quantity
#1 Shoe hello Adidas 1
#2 Jeans ahola Levis 1
#3 Jeans hello Levis 3
#4 Shoe ahola Nike 1
#5 Shoe hello Nike 5
#6 Jeans hello Spykar 2
You can also try the data.table
option:
setDT(df)[, .(Sum.Quantity = sum(Quantity)), by = list(Type, Mode, Company)]
# Type Mode Company Sum.Quantity
#1: Shoe hello Nike 5
#2: Jeans hello Levis 3
#3: Shoe hello Adidas 1
#4: Jeans hello Spykar 2
#5: Shoe ahola Nike 1
#6: Jeans ahola Levis 1
Similarly with dplyr
df %>%
group_by(Type, Mode, Company) %>%
summarise(sum(Quantity))
DATA
dput(df)
structure(list(Quantity = c(1L, 1L, 2L, 3L, 1L, 1L, 2L, 1L, 1L
), Type = structure(c(2L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L), .Label = c("Jeans",
"Shoe"), class = "factor"), Mode = structure(c(2L, 2L, 2L, 2L,
2L, 2L, 2L, 1L, 1L), .Label = c("ahola", "hello"), class = "factor"),
Company = structure(c(3L, 3L, 2L, 3L, 2L, 1L, 4L, 3L, 2L), .Label = c("Adidas",
"Levis", "Nike", "Spykar"), class = "factor")), .Names = c("Quantity",
"Type", "Mode", "Company"), class = "data.frame", row.names = c(NA,
-9L))