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

## R basics

Set two datasets (the two datasets must have identical variables)

total<-rbind(a,b)

Keep the rows I want.

subset(products, profit > 12)

Summarize()

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

## How to create a dataset that includes aggregate scales and other variables of choice

https://stackoverflow.com/questions/57023935/r-question-how-to-do-a-calculation-of-a-mean-off-multiple-columns-and-select-va/57023959#57023959

``````x2 <- transmute(mydata, circumference,
average_2items = rowMeans(cbind(age, circumference)),
age)``````

My example that worked.

> x2<-transmute(time1data,commonID,
+ GrowthMindset=rowMeans(cbind(q0008_0001, q0008_0002, q0008_0003, q0008_0004, q0008_0005, q0008_0006, q0008_0007, q0008_0008)),
+ SelfEfficacy=rowMeans(cbind(q0009_0001, q0009_0002, q0009_0003, q0009_0004, q0009_0005))
+ )

## How to get a mean off multiple column values in R

In SAS, this would be:

newvariable=mean(of x1, x2, x3, x4);

Stockoverflow

https://stackoverflow.com/questions/57015735/how-to-get-a-mean-of-multiple-column-values-using-r-dplyr/57015890#57015890

R

Approach 1:

x2 <-subset(time1data,select=c(x1, x2, x3, x4, x5))
time1data\$newvar<-rowMeans(x2,na.rm=TRUE)

Approach 2

time1data\$newvar<-rowMeans(time1data[,c("q0008_0001", "q0008_0002", "q0008_0003", "q0008_0004", "q0008_0005", "q0008_0006", "q0008_0007", "q0008_0008")])

Approach 3

time1data\$newvar<-rowMeans(time1data[,c("q0008_0001", "q0008_0002", "q0008_0003", "q0008_0004", "q0008_0005", "q0008_0006", "q0008_0007", "q0008_0008")],na.rm=TRUE)

## My R function didn't work

ネットを参考にやってみました。

これがマクロでないもの。

FUNCTIONというのをつかえばいいかなと思ってやってみましたが、ダメでした、、。

}

## R -- the merge functions

Inner join:

Keep only when both datasets provide the data for the subject/row

merge(x=demographics, y=shipping,
by.x = name, by.y="name")

merge(x= demographics, y= shipping,
by="name")

#merge another way
#full join
kaz1<- merge(x=old,y=new, by ="STUID", all=TRUE) #left join kaz2<- merge(x=old,y=new, by ="STUID", all.x=TRUE)

## How to export an Excel file (sheet) in R

The package openxlsx allows an easy deletion of existing Excel files and sheets.

library(openxlsx)

write.xlsx(x, "temp.xlsx", sheetName="merged data",
col.names=TRUE, row.names=TRUE, append=TRUE,overwrite=TRUE)

***

This below is about xlsx package.  It didn't work well when there are already existing files of the same name.  I couldn't find ways to override.

x is the name of a R dataset.

library(xlsx)

write.xlsx(x, "temp.xlsx", sheetName="merged data",
col.names=TRUE, row.names=TRUE, append=FALSE)

https://cran.r-project.org/web/packages/xlsx/xlsx.pdf

http://www.sthda.com/english/wiki/r-xlsx-package-a-quick-start-guide-to-manipulate-excel-files-in-r