Translating Stata to R: collapse
Here is the Stata output for your sample data which is identical to the data.table
output:
collapse (min) min_protein = protein ///
(mean) avg_protein = protein ///
(median) median_protein = protein ///
(sd) sd_protein = protein ///
if protein > 2.8, by(lactatio)
lactatio min_protein avg_protein median_protein sd_protein
1 2.9 3.162632 3.1 0.2180803
2 2.9 3.304688 3.25 0.2858736
3 2.9 3.371429 3.35 0.4547672
4 2.9 3.23125 3.2 0.3419917
5 2.9 3.855556 3.2 1.908606
6 3 3.2 3.1 0.2645752
7 3.3 3.65 3.65 0.4949748
8 3.2 3.3 3.3 0.1414214
and here is the data.table
output (just to make you sure that I am using the right data)
library(foreign) #reading Stata data
data<-read.dta("p004.dta")
setkey(setDT(data),lactatio)
setDT(data)[protein>2.8,
.(min_protein=min(protein),
avg_protein=mean(protein),
median_protein=median(protein),
sd_protein=sd(protein)),
by=lactatio]
lactatio min_protein avg_protein median_protein sd_protein
1: 1 2.9 3.162632 3.10 0.2180803
2: 2 2.9 3.304688 3.25 0.2858736
3: 3 2.9 3.371429 3.35 0.4547672
4: 4 2.9 3.231250 3.20 0.3419917
5: 5 2.9 3.855556 3.20 1.9086061
6: 6 3.0 3.200000 3.10 0.2645751
7: 7 3.3 3.650000 3.65 0.4949748
8: 8 3.2 3.300000 3.30 0.1414214
>