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)

 

CategoriesR

Leave a Reply