自动计算数据框的摘要统计信息并创建新表

Haa*_*kas 4 r dplyr

我有以下数据帧:

col1 <- c("avi","chi","chi","bov","fox","bov","fox","avi","bov",
          "chi","avi","chi","chi","bov","bov","fox","avi","bov","chi")
col2 <- c("low","med","high","high","low","low","med","med","med","high",
          "low","low","high","high","med","med","low","low","med")
col3 <- c(0,1,1,1,0,1,0,0,0,0,0,0,1,1,1,1,0,1,0)

test_data <- cbind(col1, col2, col3)
test_data <- as.data.frame(test_data)
Run Code Online (Sandbox Code Playgroud)

我想最终得到像这个表(值是随机的):

Species  Pop.density  %Resistance  CI_low  CI_high   Total samples
avi      low          2.0          1.2     2.2       30
avi      med          0            0       0.5       20
avi      high         3.5          2.9     4.2       10
chi      low          0.5          0.3     0.7       20
chi      med          2.0          1.9     2.1       150
chi      high         6.5          6.2     6.6       175
Run Code Online (Sandbox Code Playgroud)

%电阻列基于上面的col3,其中1 =耐受,0 =不耐受.我尝试过以下方法:

library(dplyr)
test_data<-test_data %>%
  count(col1,col2,col3) %>%
  group_by(col1, col2) %>%
  mutate(perc_res = prop.table(n)*100)
Run Code Online (Sandbox Code Playgroud)

我尝试了这个,它几乎可以解决这个问题,因为我得到了col1和2中每个值的总数为1和0的百分比,但是总样本是错误的,因为我计算所有三列,当时正确的计数仅适用于col1和2.

对于置信区间,我将使用以下内容:

binom.test(resistant samples,total samples)$conf.int*100
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何与其他人一起实施它.有一种简单快捷的方法吗?

Fra*_*ank 6

我会做...

library(data.table)
setDT(DT)

DT[, { 
  bt <- binom.test(sum(resists), .N)$conf.int*100
  .(res_rate = mean(resists)*100, res_lo = bt[1], res_hi = bt[2], n = .N)
}, keyby=.(species, popdens)]

    species popdens  res_rate    res_lo    res_hi n
 1:     avi     low   0.00000  0.000000  70.75982 3
 2:     avi     med   0.00000  0.000000  97.50000 1
 3:     bov     low 100.00000 15.811388 100.00000 2
 4:     bov     med  50.00000  1.257912  98.74209 2
 5:     bov    high 100.00000 15.811388 100.00000 2
 6:     chi     low   0.00000  0.000000  97.50000 1
 7:     chi     med  50.00000  1.257912  98.74209 2
 8:     chi    high  66.66667  9.429932  99.15962 3
 9:     fox     low   0.00000  0.000000  97.50000 1
10:     fox     med  50.00000  1.257912  98.74209 2
Run Code Online (Sandbox Code Playgroud)

包括所有级别(物种和人口密度的组合)......

DT[CJ(species = species, popdens = popdens, unique = TRUE), on=.(species, popdens), {
  bt <- 
    if (.N > 0L) binom.test(sum(resists), .N)$conf.int*100 
    else NA_real_
  .(res_rate = mean(resists)*100, res_lo = bt[1], res_hi = bt[2], n = .N)    
}, by=.EACHI]

    species popdens  res_rate    res_lo    res_hi n
 1:     avi     low   0.00000  0.000000  70.75982 3
 2:     avi     med   0.00000  0.000000  97.50000 1
 3:     avi    high        NA        NA        NA 0
 4:     bov     low 100.00000 15.811388 100.00000 2
 5:     bov     med  50.00000  1.257912  98.74209 2
 6:     bov    high 100.00000 15.811388 100.00000 2
 7:     chi     low   0.00000  0.000000  97.50000 1
 8:     chi     med  50.00000  1.257912  98.74209 2
 9:     chi    high  66.66667  9.429932  99.15962 3
10:     fox     low   0.00000  0.000000  97.50000 1
11:     fox     med  50.00000  1.257912  98.74209 2
12:     fox    high        NA        NA        NA 0
Run Code Online (Sandbox Code Playgroud)

这个怎么运作

语法是DT[i, j, by=]......

  • i确定行的子集,有时使用辅助参数,on=roll=.
  • by=确定子集化表中的组,切换到keyby=排序.
  • j 是代码作用于每个组.

j应该评估一个列表,.()作为一个快捷方式list().详情?data.table请见.

使用的数据

(重命名列,将重新格式化的二进制变量返回到0/1或false/true,按正确的顺序设置人口密度级别):

DT = data.frame(
  species = col1, 
  popdens = factor(col2, levels=c("low", "med", "high")), 
  resists = col3
)
Run Code Online (Sandbox Code Playgroud)