I have the following data
library(reshape2)
d <- data_frame(
Quart = c("Q1", "Q2", "Q3", "Q4", "Q4", "Q1","Q4","Q1", "Q2", "Q3"),
var1 = c(1,0,1,1,1,0,2,3,0,1),
var2= c(1,0,1,1,1,0,1,0,1,0),
location= c("loc1", "loc1", "loc2", "loc2", "loc2", "loc2", "loc2",
"loc1", "loc2","loc1"))
I would like to reshape these data to create a table to look like the below.
The cell that is starred should be var1 multiplied by the number of loc1 where Quart=Q1. So in this scenario it would be '3', loc2 would be '0' as there aren't any rows that have both Q1 and loc2, loc3 would be '1', and loc4 would be '0'.
I've tried to reshape the data using d cast:
d%>%
reshape2::dcast(formula = Quart * var1 ~ location
,fun.aggregate = length
translates to counts
,margins = c("Quart", "var1")
) -> d
However this doesn't quite give me what I want. Any ideas?
Thanks