Testing Data Table
library(data.table)
Create Data Table
DT <- data.table(a=c("NZ", "AU"),
b=c("com", "com", "com", "com", "com", "net", "net", "net", "org", "com", "com", "com"),
c=c(1L,2L),
d=1:12,
e=round(rnorm(4), 2))
DT[, .N] #number of rows
## [1] 12
DT
## a b c d e
## 1: NZ com 1 1 0.11
## 2: AU com 2 2 -0.34
## 3: NZ com 1 3 -0.16
## 4: AU com 2 4 0.69
## 5: NZ com 1 5 0.11
## 6: AU net 2 6 -0.34
## 7: NZ net 1 7 -0.16
## 8: AU net 2 8 0.69
## 9: NZ org 1 9 0.11
## 10: AU com 2 10 -0.34
## 11: NZ com 1 11 -0.16
## 12: AU com 2 12 0.69
Count and relative frequency of rows on groups created with c and a columns
DT_count<-DT[, .(Count =.N), by=.(a, b)]
DT_count[, Quota.b_on_a:=round((Count*100/sum(Count)),1), by=a]
DT_count[, Quota.a_on_b:=round((Count*100/sum(Count)),1), by=b]
DT_count[order(a)][]
## a b Count Quota.b_on_a Quota.a_on_b
## 1: AU com 4 66.7 50.0
## 2: AU net 2 33.3 66.7
## 3: NZ com 4 66.7 50.0
## 4: NZ net 1 16.7 33.3
## 5: NZ org 1 16.7 100.0
.. or chaining (no need to create a new DT)
DT[,.(Count =.N), by=.(a, b)][,Quota.b_on_a:=round((Count*100/sum(Count)),1), by=a][,Quota.a_on_b:=round((Count*100/sum(Count)),1), by=b][order(a)][]
## a b Count Quota.b_on_a Quota.a_on_b
## 1: AU com 4 66.7 50.0
## 2: AU net 2 33.3 66.7
## 3: NZ com 4 66.7 50.0
## 4: NZ net 1 16.7 33.3
## 5: NZ org 1 16.7 100.0