问题
I have a data.frame like this (example):
product protein fat starch
aaa 40 5 10
bbb 50 6 8
ccc 12 50 4
and I want to ask for a summary of this values (min,max,1stQ, 3rdQ..). When I run:
aggregate(protein~product,summary,data=DATA4, na.rm = TRUE)
I have this...
product protein.Min. protein.1st Qu. protein.Median protein.Mean protein.3rd Qu. protein.Max.
aaa 6.400 14.700 15.600 15.540 16.600 22.500
bbb 6.300 9.400 10.100 10.130 10.800 15.100
ccc 23.000 24.080 24.250 24.180 24.420 25.000
However I also wanted to have the frequency and SD. How can I ask that? I tried with ddply but i cannot make it works. (I have NA's in some variables(protein, fat, starch...)
Besides this, and because here i'm only asking a summary for protein levels, how can I ask a summary for every variables that I have (protein, fat, starch, etc...) all in once?
Thank you very much!
回答1:
If I want to specify how I get the output of a summary I usually turn to a more elaborate solution using dplyr
like so:
library(dplyr)
df <- data.frame(product = rep(letters[1:3], each=3,3),
protein = sample(10:40, 27, replace=T))
df %>% group_by(product) %>%
summarise(min = min(protein)
,max = max(protein)
,mean = mean(protein)
,sd = sd(protein)
,n = n()
,q25 = quantile(protein, .25)
,q75 = quantile(protein, .75))
result:
# A tibble: 3 × 8
product min max mean sd n q25 q75
<fctr> <int> <int> <dbl> <dbl> <int> <dbl> <dbl>
1 a 16 39 24.66667 8.717798 9 17 30
2 b 24 40 31.55556 5.387743 9 26 35
3 c 13 38 26.66667 8.108637 9 22 31
来源:https://stackoverflow.com/questions/42004912/add-frequency-and-sd-to-a-summary-in-r