问题描述
我有一个像这样的data.frame(示例):
I have a data.frame like this (example):
product protein fat starch
aaa 40 5 10
bbb 50 6 8
ccc 12 50 4
,我想问一下这些值的摘要(最小值,最大值,1stQ,3rdQ ..).当我运行时:
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)
我有这个...
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
但是我也想知道频率和SD.我该怎么问?我尝试了ddply,但无法使其正常工作.(我在某些变量(蛋白质,脂肪,淀粉等)中都具有不适用值
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?
非常感谢!
推荐答案
如果我想指定如何获取摘要的输出,我通常会使用 dplyr
寻求更为详尽的解决方案,如下所示:
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))
结果:
# 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
这篇关于将频率和SD添加到R中的摘要中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!