I need to subset a data frame into smaller data frames by age. I want to write a function for this but do not know how to avoid writing each variable name.
Below is how I have accomplished this in the past:
CompletedStudy <- StudyCompletion %>%
subset(complete==1)
CompletedStudy_Under24months <- CompletedStudy %>%
subset(child_age<24)
CompletedStudy_Over24months <- CompletedStudy %>%
subset(child_age>=24)
I want to create something like the below function:
CompletedStudy <- StudyCompletion %>%
subset(complete==1)
AgeCompletion <- function(x) {
x+"_Under24Months" <- x %>%
subset(child_age<24)
x+"_Over24Months" <- x %>%
subset(child_age>=24)
}
AgeCompletion(CompletedStudy)
Is this possible?