R function example -- when a data frame is involved

var.labels = c(age="Age in Years", GPA_12_GRADE="Test Score", gpa="GPA", sex="Sex of the participant")
DATA_KAZ <- data.frame(age = c(21, 30, 25, 41, 29, 33,NA),
GPA_12_GRADE = c(75, 65, 88, 92, 97, 79, 83),
gpa = c(3.4,2.7,3.9,4.0,2.5,1.2, 3.2),
sex = factor(c(1, 2, 1, 2, 1, 2, 1), labels = c("Female", "Male")),
race=factor(c(1,1,2,2,3,3, 1), labels = c("White", "Black","Hispanic")))
label(DATA_KAZ) = as.list(var.labels[match(names(DATA_KAZ), names(var.labels))])

CCC<-filter(DATA_KAZ,sex=="Female")
TTT<-filter(DATA_KAZ,sex=="Male")

 

kaz_macro<-function(kaz1){
col_name <- deparse(substitute(kaz1))
C_mean<-mean(CCC[[col_name]])
T_mean<-mean(TTT[[col_name]])
C_sd<-sd(CCC[[col_name]])
T_sd<-sd(TTT[[col_name]])
C_n<-length(CCC[[col_name]])
T_n<-length(TTT[[col_name]])

simple_gap=T_mean-C_mean
g1<- ((T_n-1)*(T_sd*T_sd))+((C_n-1)*(C_sd*C_sd))
g2= T_n + C_n -2
g3= sqrt(g1/g2)
wwc_effect= simple_gap/g3
wwc_effect
}
kaz_macro(GPA_12_GRADE)
kaz_macro(gpa)

Leave a Reply