How to write a function to do these?

I have a repetitive process in R and I want to get this done in one function.  Could you help?

result1<-Summarize(GrowthMindset ~ treat+ dataID, data=wholedata2)
result1$varname<-"GrowthMindset"
result1$sample_type<-"Whole"

#Scale 2
result2<-Summarize(SelfEfficacy ~ treat+ dataID, data=wholedata2)
result2$varname<-"SelfEfficacy"
result2$sample_type<-"Whole"

#Scale 3
result3<-Summarize(MSelfEfficacy ~ treat+ dataID, data=wholedata2)
result3$varname<-"MSelfEfficacy"
result3$sample_type<-"Whole"

#Scale 4
result4<-Summarize(MathAnxiety ~ treat+ dataID, data=wholedata2)
result4$varname<-"MathAnxiety"
result4$sample_type<-"Whole"

#Scale 5
result5<-Summarize(TeacherUse ~treat+  dataID, data=wholedata2)
result5$varname<-"TeacherUse"
result5$sample_type<-"Whole"

 

The solution (thanks Isaac):

kaz_macro2<-function(var1){
x1<- Summarize(GrowthMindset ~ treat+ dataID, data=var1)
x2<- Summarize(SelfEfficacy ~ treat+ dataID, data=var1)
x3<- Summarize(MSelfEfficacy ~ treat+ dataID, data=var1)
x4<- Summarize(MathAnxiety ~ treat+ dataID, data=var1)
x5<- Summarize(TeacherUse ~ treat+ dataID, data=var1)
as.data.frame(rbind(x1,x2,x3,x4,x5))
}
temp1234<-kaz_macro2(wholedata2)

 

 

CategoriesR

Leave a Reply