Firstly I aplogise because this question has no doubt been answered in another page eg How do I use a macro variable in R? (Similar to %LET in SAS) and Pass string as name of attached data column name but I’ve read them, watched Youtube tutorials, thought that the function command was what I wanted and I just can’t get it to work. I’m very new to programming as a whole, and only few days in for R, and I’m sure I’m missing something due to my lack of experience or understanding of even where to start.
In SAS I would do it like this
%Macro DV (var1, var2);
Proc mixed …
Model &var1 = &var2 …
Data &var1_&var2…
%mend DV;
%DV (VAS, Hyd)
%DV (SOM, Hyd)
%DV (GAS, Hyd)
The variables (eg. VAS, Hyd) that I’m calling in are column labels, they get called in, the column analysed in a mixed model and, with a bit more code, the dataset gets exported with a filename according to the variables analysed. I’m not trying to do a lmer in R I’m using the rmcorr package and want to change the columns that get analysed. The original expression, that works, looks like
VASCor.rmc <- rmcorr(participant = Subject, measure1 = Hyd, measure2 = VAS, dataset = mydata)
plot(VASCor.rmc,mydata, overall = T, lty=1, xlab = "Hyd", ylab = "VAS")
A working dataset can be found below. So just working on the first expression I tried the following, thinking that the var1 and var2 would get inserted in the correct places but I just end up with an error "object 'var2' not found".
Allrmcorr <- function(var1, var2){
var1.var2.rmc <- rmcorr(participant = Subject, measure1 = var2, measure2 = var1 , dataset = Results2)
}
Allrmcorr("VAS","Hyd")
Allrmcorr("VAS","Hyd")
I've attempted a number of expressions and probably spent long enough trying to figure it out to have just rewritten the expression time, I'm not closer but being stubborn I want to know the better way for next time. Any help would be greatly appreciated.
mydata <- data.frame(
Subject = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3),
Time = c(1:5,1:5,1:5),
VAS= runif(15),
Hyd =runif(15),
GAS= runif(15)
)