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)

 

 

R function not working (but I got an answer on Reddit)

以下、誰のPCでも走るようにサンプルを書きました。

Rですが、私自身が書いたFUNCTIONでなぜ、result_dataという名のデータが、SAVEされないのかが分かりません。アドバイスお願いします。(回答はREDDITでいただきました。最後のところに書き込みました)。

最初のところで仮のデータを作る部分がややこしくなっていますが、たまたま仕事で同僚が書いてくれたものです。

library(dplyr)
library(tidyverse)

 

#create fake data
junk1 <- c(1, 2, 2, 4, 3, 3, 4, 1)
junk1 <- data.frame(matrix(junk1,ncol=2, byrow=T))
colnames(junk1) <- c("id", "num")
junk1$j1_flag <- 1
library(data.table)
junk1 <- data.table(junk1)
junk1$id <- as.factor(junk1$id)
junk2 <- c(1, "F", 2, "M", 5, "F")
junk2 <- data.frame(matrix(junk2,ncol=2, byrow=T))
colnames(junk2) <- c("id", "gender")
junk2$j2_flag <- 1
junk2 <- data.table(junk2)

#merge
junk1_2<-merge(junk1,junk2,by='id', all=TRUE)

 

#以下が問題の部分です。なぜresult_dataという名のデータがSAVEされないのかがわかりません。

kaz_macro1<-function(var1,var2){
var1 %>%
mutate(newvar = j2_flag+100) ->var2
}
kaz_macro1(junk1_2,result_data)

 

REDDITで教えてもらいました。ファンクションの外でデータとしてセーブするように指定するとできました。

kaz_macro1<-function(var1){
var1 %>%
mutate(newvar = j2_flag+100)
}

result_data2<-kaz_macro1(junk1_2)

R mutate case_when NOT WORKING (Solved)

なぜか、太字のところしか機能しておらず、結果は6か、NULLになってしまいます。行ごとに、前の結果が消されていっているために最後の6しか残らないのだと思います。

wholedata %>%
mutate(year_ID = case_when(
REF_DATE >= year1start & REF_DATE <= year1end ~1,
REF_DATE >= year2start & REF_DATE <= year2end ~2,
REF_DATE >= year3start & REF_DATE <= year3end ~3,
REF_DATE >= year4start & REF_DATE <= year4end ~4,
REF_DATE >= year5start & REF_DATE <= year5end ~5,
REF_DATE >= year6start & REF_DATE <= year6end ~6,
TRUE ~ -9)-> wholedata2

 

解決しました。最後の行の)が足りませんでした。

wholedata %>%
mutate(year_ID = case_when(
REF_DATE >= year1start & REF_DATE <= year1end ~1,
REF_DATE >= year2start & REF_DATE <= year2end ~2,
REF_DATE >= year3start & REF_DATE <= year3end ~3,
REF_DATE >= year4start & REF_DATE <= year4end ~4,
REF_DATE >= year5start & REF_DATE <= year5end ~5,
REF_DATE >= year6start & REF_DATE <= year6end ~6,
TRUE ~ -9)) -> wholedata2

R question

Someone gave me am example of how to write a simple function. This was great.

addition = function(num1,num2){
answer = num1+num2
return(answer)
}
addition(10,9)
addition(5,4)

However, when I tried to use it, I realized I want to use a text instead of a number in this way.

my_matching = function(dataname){
m.out1<-matchit(data=dataname,control~gpa,method="nearest",ratio=1,
m.order="random", caliper=0.25)
}
my_matching(ABC_data1x)
my_matching(ABC_data2x)

Could you advise how I can run this without an error?

Also, how about a case like this? I used $ and . to indicate which part has to be replaced but this is not R way (This would be in SAS).

my_matching = function(data_N){
m.out1<-
matchit(data=ABC_data&data_n.x,control~gpa,method="nearest",ratio=1,
m.order="random", caliper=0.25)
}
my_matching(1)
my_matching(2)

Thank you!

R package matchit

Example using a R default data matcars.

library(MatchIt)
m.out0<-matchit(data=mtcars,am~hp+drat,exact=c("vs"),method="nearest", ratio=1, m.order="random", caliper=0.25)
m.out0

Using R to do regular data things

# SINGLE AGGREGATE
#sapply(abc[c("GrowthMindset", "SelfEfficacy", "MSelfEfficacy","MathAnxiety","TeacherUse")], mean)

#this just gets means
sapply(abc[c("GrowthMindset", "SelfEfficacy", "MSelfEfficacy","MathAnxiety","TeacherUse")], function(x) mean(x, na.rm=TRUE))

#this gives me a matrix of means
aggregate(cbind(GrowthMindset, SelfEfficacy, MSelfEfficacy) ~ treat, abc, function(x) mean(x, na.rm=TRUE))-> result1

#this gets me a full results
aggregate(cbind(GrowthMindset, SelfEfficacy, MSelfEfficacy, MathAnxiety,TeacherUse) ~ treat, abc,
function(x) c(sum=sum(x), mean=mean(x), min=min(x), q1=quantile(x)[2],
median=median(x), q3=quantile(x)[4], max=max(x), sd=sd(x)))