I'm trying to use as.name(x) to refer to a list to input into a function. Here's an example of my simplified version of my stats function followed by the for loop I'm using to output all the data at once.
get<-function(data,x) {
for (i in x) {
lm(as.formula(paste(i,'~',variable)),data)
}
}
lists<-c("a","b","c")
# where each of a, b, and c are lists that refer to column names of my data
for (j in lists) {
get(data,as.name(j))
}
I keep getting the following error:
Error in for (i in x) { : invalid for() loop sequence
If I just do get(data,a) each time it works but not when I try and do a loop.
Are each of a
, b
and c
a list that contains only one value? I ask because your lm()
formula has i
on the left hand side, and can only be a vector.
If that's the case, then replacing as.name(j)
with j
should make your code work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With