R function example

library(broom)
library(compute.es)
library(dplyr)
library(forcats)
library(FSA)
library(gapminder)
library(ggplot2)
library(gmodels)
library(haven)
library(here)
library(leaflet)
library(magrittr)
library(markdown)
library(MatchIt)
library(plyr)
library(psych)
library(purrr)
library(readr)
library(readxl)
library(sas7bdat)
library(sf)
library(sqldf)
library(stringr)
library(summarytools)
library(tidyverse)
library(tmap)
library(tmaptools)
library(descr)
library(writexl)

library(tidyverse)

table1

show_example<-function(var1,var2){

var1_d <-deparse(substitute(var1))
var2_d <-deparse(substitute(var2))

table1 %>%
mutate(prop_cases=table1[[var1_d]]/table1[[var2_d]]) -> temp123
Summarize(temp123$prop_cases ~ country, data= temp123) -> kaz

}

result1<-show_example(var1=cases,var2=population)
result2<-show_example(var1=cases,var2=cases)

R Function example -- from Datacamp.com

https://campus.datacamp.com/courses/introduction-to-writing-functions-in-r/all-about-arguments?ex=2

 

# Set the default for n to 5
cut_by_quantile <- function(x, n, na.rm, labels, interval_type) {
probs <- seq(0, 1, length.out = n + 1)
qtiles <- quantile(x, probs, na.rm = na.rm, names = FALSE)
right <- switch(interval_type, "(lo, hi]" = TRUE, "[lo, hi)" = FALSE)
cut(x, qtiles, labels = labels, right = right, include.lowest = TRUE)
}

# Remove the n argument from the call
cut_by_quantile(
n_visits,
n = 5,
na.rm = FALSE,
labels = c("very low", "low", "medium", "high", "very high"),
interval_type = "(lo, hi]"
)

R

When two variables are similar but different by training empty spaces, do this.

levels(as.factor(main$county))

levels(as.factor(arc$county))

I tried to merge two data tables that contain the same variable county.   The simple merge did not work.  It turns out that one variable had trailing empty spaces.

R string macro

https://notstatschat.rbind.io/2018/07/30/quoting-and-macros-in-r/

Also

#id dups
#create fake data
junk <- c(1, "J", 1, "J", 1, "K", 2, "K", 3, "K", 3, "T", 3, "T", 3, "T")
junk_mat <- data.frame(matrix(junk,ncol=2, byrow=T))
#install.packages("data.table") # install it
library(data.table)
junk_tab <- data.table(junk_mat)
colnames(junk_tab) <- c("num_var", "char_var")
junk_tab$num_var <- as.numeric(junk_tab$num_var)
junk_tab

library(psych)

#this one works with char_var in quotes
kaz_macro1<-function(var1,var2){
var1<-describeBy(x=as.numeric(junk_tab$num_var), group=junk_tab[[var2]])
print(var1)
}
kaz_macro1(var1=kc1a,var2="char_var")

#this one works without char_var in quotes
kaz_macro1<-function(var1,var2){
temp_var <-deparse(substitute(var2))
var1<-describeBy(x=as.numeric(junk_tab$num_var), group=junk_tab[[temp_var]])
print(var1)
}
kaz_macro1(var1=kc1a,var2=char_var)

 

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)