I have successfully created a dynamic variable inside a for loop. I want to perform a simple one sample ttest on the created variable. How do I do that within the loop/ dynamically? This is what I have written -
for (i in 1:15) {
# create a dynamic variable name
df_nam <- paste("df", i, sep = "_")
# give it values
assign(df_nam, df[df$k == i, ])
# create variable that would hold the results of ttest
ttest_nam <- paste("ttest_k", i, sep = "_")
# what do I put here??
assign(ttest_nam, as.formula(paste("t.test(", df_nam$result, ")")))
}
The way it currently is, given me this error - Error in class(ff) <- "formula" : attempt to set an attribute on NULL
. Is there a way to even do this, or will I have to perform the ttest individually?
I worry that when the length is large (more than 15 as it currently is), writing each set of tests down would be inviting errors.