简体   繁体   English

R:使用dplyr函数创建函数

[英]R: Creating a function using dplyr functions

I have a data frame with three variables of interest: 我有一个包含三个感兴趣变量的数据框:

  • survival time 生存时间
  • grouping factor 分组因子
  • event indicator (dead: yes or no) 事件指示器(死:是或否)

I want to calculate incidence rate for each group. 我想计算每个组的发生率。 I do this daily, so it would be great to have a function doing this instead of a long script. 我每天都这样做,所以有一个函数来执行此操作而不是冗长的脚本将是很棒的。

I've tried the following, but doesn't work. 我已经尝试了以下方法,但是不起作用。

library(survival)
data(lung) # example data
lung$death <- ifelse(lung$status==1, 0, 1) # event indicator: 0 = survived; 1 = dead.

# Function
func <- function(data_frame, group, survival_time, event) {
     library(epitools)
     table <- data_frame %>%
          filter_(!is.na(.$group)) %>%
          group_by_(.$group) %>%
          summarise_(pt = round(sum(as.numeric(.$survival_time)/365.25)),
                     events = sum(.$event)) %>%
          do(pois.exact(.$events, pt = .$pt/1000, conf.level = 0.95)) %>%
          ungroup() %>%
          transmute_(Category = c(levels(as.factor(.$group))),
                     Events = x,
                     Person_years = pt*1000,
                     Incidence_Rate = paste(format(round(rate, 2), nsmall=2), " (",
                                      format(round(lower, 2), nsmall=2), " to ",
                                      format(round(upper, 2), nsmall=2), ")", 
                                      sep=""))
     return(table)
}

func(lung, sex, time, death)

**Error: incorrect length (0), expecting: 228 In addition: Warning message:
In is.na(.$group) : is.na() applied to non-(list or vector) of type 'NULL'**

Any ideas? 有任何想法吗? I've read the post about NSE and SE in dplyr, but thought I applied the recommendations correctly? 我已经在dplyr中阅读了有关NSE和SE的帖子,但以为我正确应用了建议?

Here is a part of the solution 这是解决方案的一部分

data_frame = lung
group = "sex"
survival_time = "time"
event = "death"
data_frame %>%
  filter_(paste("!is.na(", group, ")")) %>%
  group_by_(group) %>%
  summarise_(
    pt = paste("round(sum(as.numeric(", survival_time, ") / 365.25))"),
    events = paste("sum(", event, ")")
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM