In R, assume there is a data frame “mydata” comprising the independent variable “group” (two groups) and n dependent variables. I want to run n t-tests (one for each dependent variable), print and save the results in a file by doing the following:
variables<-names(mydata)
variables<-variables[-1] #to remove the group variable
capture.output(for(i in variables){print(t.test(get(paste("mydata$", i, sep=""))~mydata$group))},file="outputfile.txt")
This command returns the following error message:
Error in get(paste("mydata$", i, sep = "")) : object 'mydata$variable1' not found
What am I doing wrong?
For others, some sample data:
I’d use the much more straight forward:
if I were using a for loop. However, you could do this using a more R-like style: