After some time away from R
I feel I am making very clumsy code to get basic summary statistics in data.table
.
What I am doing is finding proportion of individuals in good/bad health conditional on species.
# Some data
n = 300
set.seed(2)
dt <- data.table(type = sample(x = c("Dog", "Cat", "Horse"), size = n, replace = TRUE),
health = sample(x = c("Good", "Bad"), size = n, replace = TRUE))
# Making the table. In a clumsy manner?
dt.fr <- dt[, .N, .(type, health)][, perc.type := N/sum(N)*100,
by = type][order(type, health)]
dt.fr
type health N perc.type
1: Cat Bad 38 44.70588
2: Cat Good 47 55.29412
3: Dog Bad 56 50.90909
4: Dog Good 54 49.09091
5: Horse Bad 61 58.09524
6: Horse Good 44 41.90476
How would I produce the table above with more elegant code?