Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid for() loop sequence

Tags:

r

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.

like image 858
George Avatar asked Nov 08 '22 18:11

George


1 Answers

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.

like image 191
rdodhia Avatar answered Nov 15 '22 07:11

rdodhia