我已经对我的数据集进行了分析,但分析形式很长,我想编写一个函数来自动化必须多次编写的零件。
#Null_proband outputs for disease tables
null.proband <- dbdata %>%
filter(Proband == TRUE & Fam.state.mainmut.MutGroup == "Null") %>%
group_by(ICD_Grouping) %>%
summarise(n_distinct(PID),
n_distinct(Fam.state.mainmut.FID),
n(),
median(AgeOfOnset))
#Column names for null_proband
colnames(null.proband) = c("ICD_Grouping", "proband_uniq(PID)", "proband_uniq(FID)", "proband_freq(PID)","proband_median(ageofonset)")
对于不同的过滤集,我多次重复此操作,我想知道我是否可以使用带有if子句的基本功能
f1 <- function(x, pop = c("proband", "nonproband", "carrier", "unknown", "all), mut = ("null", "mis", "unk", "all"))
这就是我希望为函数设置的基本dplyr进程
{ pop.mut <- dbdata %>%
filter(defined by arguments) %>%
group_by(ICD_Grouping) %>%
summarise(pop_uniqPID = n_distinct(PID),
pop_uniqFID = n_distinct(FID),
pop_freqPID = n(),
pop_medianAgeOfOnset = median(AgeOfOnset))
}
然后是if子句,该子句描述了取决于参数的过滤器应该是什么,例如
if (pop = "proband" & mut = "null") { filter(Proband == TRUE & Fam.state.mainmut.Mutgroup == "Null") }
然后为所有组合编写循环,或者我可以为带有参数的过滤器的一半编写循环,或者只为一个参数过滤一次,然后为第二个参数过滤第二遍?