如何将函数的应用结果添加到现有数据框?

问题描述 投票:1回答:1

我试图计算一些费率的置信区间。我正在使用tidyverse和epitools来计算Byar方法中的CI。

我几乎肯定做错了什么。

library (tidyverse)
library (epitools)


# here's my made up data

DISEASE = c("Marco Polio","Marco Polio","Marco Polio","Marco Polio","Marco Polio",
            "Mumps","Mumps","Mumps","Mumps","Mumps",
            "Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox","Chicky Pox")
YEAR = c(2011, 2012, 2013, 2014, 2015,
         2011, 2012, 2013, 2014, 2015,
         2011, 2012, 2013, 2014, 2015)
VALUE = c(82,89,79,51,51,
          79,91,69,89,78,
          71,69,95,61,87)
AREA =c("A", "B","C")

DATA = data.frame(DISEASE, YEAR, VALUE,AREA)


# this is a simplification, I have the population values in another table, which I've merged 
# to give me the dataframe I then apply pois.byar to.
DATA$POPN = ifelse(DATA$AREA == "A",2.5,
              ifelse(DATA$AREA == "B",3,
                     ifelse(DATA$AREA == "C",7,0)))


# this bit calculates the number of things per area
rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
  count(AREA)

然后,如果我想计算CI我认为这将工作

rates<-DATA%>%group_by(DISEASE,AREA,POPN)%>%
  count(AREA) %>%
  mutate(pois.byar(rates$n,rates$POPN))

但我明白了

Error in mutate_impl(.data, dots) : 
  Evaluation error: arguments imply differing number of rows: 0, 1.

但这有效:

pois.byar(rates$n,rates$POPN)

然后说:“将pois.byar函数的结果转换为数据帧然后合并回原始数据”似乎很愚蠢。我可能试过这只是为了得到一些数据....我不想这样做。这不是正确的做事方式。

任何建议都感激不尽。我认为这是一个相当基本的问题。并且表明我不会坐着学习,而是在我去的时候尝试做事。

这就是我想要的疾病年份n区域popn x pt率低于上限conf.level

r tidyverse confidence-interval mutate
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.