我正在r中构建一个用户定义的函数。它看起来像这样(数据框只是一个例子):
library(dplyr)
library(lazyeval)
df<-data.frame(type1=c("a","b","c","a","b","c",NA),
type2=c("d","e","f","d","e","f","f"),
type3=c("d","f","g","e","f","g","e"),
type4=c("e","f","g","e","f","g","e"))
f<-function(x){
y<-df%>%
dplyr::filter_(lazyeval::interp(~!is.na(x),x=as.name(x)))%>%
dplyr::group_by_(x)%>%
dplyr::summarize("Sum2"=sum(type2=="d"),
"Sum3"=sum(type3=="d"),
"Sum4"=sum(type4=="d"))
y<-dplyr::bind_rows(y,setNames(data.frame("Total",
sum(y$Sum2),
sum(y$Sum3),
sum(y$Sum4),
paste(
round(chisq.test(y$Sum2,y$Sum3)$p.value,3),
"(Chi-sq.)")),
c(x, "Sum2","Sum3","Sum4","pvalue")))
return(y)
}
result_f<-f("type1")
问题在于我想在卡方中不仅包括两个变量而是三个变量(Sum2
,Sum3
和Sum4
),因为在这种情况下只有{{ 1}}和Sum2
是相关的,但在其他情况下,函数可能需要分析三个变量。
是否有任何方法可以包含Sum3
,即Sum 4
而不是chisq.test(y$Sum2,y$Sum3,y$Sum4)
,但是使用某种条件选择排除那些归零的变量(例如)?