df <-data.frame(id=c(1L,1L,2L,3L,2L,3L,4L),brand=c('a','a','b','c','d','a','b'),quantity=c(
2L,1L,5L,10L,11L,1L,2L),stringsAsFactors=F);
id brand quantity
1 1 a 2
2 1 a 1
3 2 b 5
4 3 c 10
5 2 d 11
6 3 a 1
7 4 b 2
this is the data frame
if I use the following command
sparse_matrix1 <- reshape2::dcast(df, df$id~df$brand )
I get the following output
df$id a b c d
1 1 2 0 0 0
2 2 0 1 0 1
3 3 1 0 1 0
4 4 0 1 0 0
It is taking only first entry for the result but it is not giving the sum of the first two entries.
Is there a way such that, I get answer as 3 (i.e. 2+1 sum of quantity for first and second entries) brand =3 and id = 1.
The output should be
df$id a b c d
1 1 3 0 0 0
2 2 0 1 0 1
3 3 1 0 1 0
4 4 0 1 0 0
Thanks in advance.