如何在R中同时使用统计函数和子集数据?

时间:2017-09-22 15:50:09

标签: r subset mean which

我的数据看起来像这样(dat)

region muscle    protein
head   cerebrum  78
head   cerebrum  56
head   petiole   1
head   petiole   2
tail   pectoral  3
tail   pectoral  4

我想取cerebrum的蛋白质值的平均值。我尝试查找不同的方法来对数据herehere进行分组。但似乎没有一种直接的方式来做到这一点。现在,我这样做:

datcerebrum <- dat[which(dat$muscle == "cerebrum"),]
mean(datcerebrum$protein)

我试着浓缩这一行:

mean(dat[which(dat$muscle == "cerebrum"),])

但它抛出一个NA,警告说参数不是数字或逻辑。有没有一种简单的方法来实现这一目标?

3 个答案:

答案 0 :(得分:4)

我使用tidyverse包dplyr执行此操作:

library(readr)
library(dplyr)
fwf <- "head   cerebrum  78
head   cerebrum  56
head   petiole   1
head   petiole   2
tail   pectoral  3
tail   pectoral  4"
dat <- read_fwf(fwf, fwf_empty(fwf, col_names = c("region", "muscle", "protein")))
# The above code is just to create your data frame - please provide reproducible data!

dat %>% filter(muscle == "cerebrum") %>% summarise(m = mean(protein))
#> # A tibble: 1 x 1
#>       m
#>   <dbl>
#> 1    67

你甚至可以同时为每一块肌肉做这件事:

dat %>% group_by(muscle) %>% summarise(m = mean(protein))
#> # A tibble: 3 x 2
#>     muscle     m
#>      <chr> <dbl>
#> 1 cerebrum  67.0
#> 2 pectoral   3.5
#> 3  petiole   1.5

答案 1 :(得分:3)

我们可以使用aggregate

中的base R
aggregate(protein ~muscle, dat, mean)
#   muscle protein
#1 cerebrum    67.0
#2 pectoral     3.5
#3  petiole     1.5

答案 2 :(得分:3)

使用data.table的解决方案:

# Load required library
library(data.table)
# Transform you data into a data.table object
setDT(dat)
# Subset cerebrum and mean protein values
data[muscle == "cerebrum"][, mean(protein)]