[Solved] R: How to make aggregate pivot table [closed]


data:

df<-
data.table::fread("id product qua    color  month
1 Box     3       red    jan  
2 Box     14      blue   jan
3 Box     22      green  jan
4 Box     10       red   feb  
5 Box     12      blue   feb
6 Box     36      green  feb
7 Box     31       red   mar  
8 Box     1      blue    mar
9 Box     7      green   mar")[,-1] %>% setDF

code:

df %>% 
mutate(color = factor(color,levels=unique(na.omit(color))), month = factor(month,levels=unique(na.omit(month)))) %>%
spread(month, qua)

result:

  product color jan feb mar
1     Box   red   3  10  31
2     Box  blue  14  12   1
3     Box green  22  36   7

To have the Box only once:

Save the above to result for e.g..

result$product[duplicated(result$product)]<-""

New result:

  product color jan feb mar
1     Box   red   3  10  31
2          blue  14  12   1
3         green  22  36   7

4

solved R: How to make aggregate pivot table [closed]