Is this a good example?.. they are all the same gvkey, and all entries have unique datadates ?
NewData<- data.frame(
gvkey = c(7041, 7041, 7041, 7041, 7041, 7041, 7041, 7041, 7041, 7041),
datadate = c(20000103,20000104,20000105,20000106,
20000111,20000112,20000113,20000114,20000117,20000118),
cshoc = c(4.2e+07,4.2e+07,4.2e+07,4.2e+07,4.2e+07,
4.2e+07,4.2e+07,4.2e+07,4.2e+07,4.2e+07),
prccd = c(3.86, 4.28, 4, 4.04, 4, 3.96, 3.92, 3.96, 4.06, 4.14)
)
mutate(NewData,
firmMarket=cshoc*prccd)%>%
group_by(gvkey,datadate)%>%
mutate(TotalMarket=sum(firmMarket,na.rm=TRUE))%>%
mutate(weightage=firmMarket/TotalMarket)
# A tibble: 10 x 7
# Groups: gvkey, datadate [10]
gvkey datadate cshoc prccd firmMarket TotalMarket weightage
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 7041 20000103 42000000 3.86 162120000 162120000 1
2 7041 20000104 42000000 4.28 179760000 179760000 1
3 7041 20000105 42000000 4 168000000 168000000 1
4 7041 20000106 42000000 4.04 169680000 169680000 1
5 7041 20000111 42000000 4 168000000 168000000 1
6 7041 20000112 42000000 3.96 166320000 166320000 1
7 7041 20000113 42000000 3.92 164640000 164640000 1
8 7041 20000114 42000000 3.96 166320000 166320000 1
9 7041 20000117 42000000 4.06 170520000. 170520000. 1
10 7041 20000118 42000000 4.14 173880000 173880000 1