## Simple IF statement in R

I haven't tested this yet.

In SAS:

If A=2 then B=3;

In R:

data[which(A==2),"B"] = 3

or

data[which(data[,"A"]==2),"B"] = 3

## R gmodels CrossTable

CrossTable(juvs2\$var1,juvs2\$va2,prop.r=TRUE,prop.c=FALSE,prop.t=FALSE,digit=2,format="SPSS",prop.chisq=FALSE)

## Combine two string variables using R

library(tidyverse)

library(stringr)

court2qe\$DIV_PAP_RESULT<-str_c(court2qe\$"diverted_case",court2qe\$"papered_case")
freq(court2qe\$DIV_PAP_RESULT)

## 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)

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)

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

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.

}

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!

## How to select just 10 observations from a R dataset

The first line creates a sequence variable.  The second like selects the first 30 observations.

smalldata\$suji <- 1:nrow(smalldata)
smalldata<-filter(smalldata,suji <= 30)

In SAS's datastep, this would be:

suji=_n_;

if suji <=30;

## R cross tab

library(gmodels)

CrossTable(smalldata\$control, smalldata\$male)