1

I am new to R , and even if I searched for a solution , I haven't found it yet. I want to call in a loop an plmtest object. I assign the results from Lagrange Multiplier Test - (Breusch-Pagan) in plmtest5 and plmtest6, with the following syntax in R:

for (i in (5:6))  assign(paste("plmtest",i,sep=""),
plmtest(eval(parse(text=paste('pool',i, sep=""))),type=c("bp")))

I want to show the results in a loop. I've tried

for (i in (5:6)) paste("plmtest",i,sep="") 

and

for (i in (5:6)) print(paste("plmtest",i,sep=""))

with no results. The plmtest5 and plmtest6 works, but I want them in a loop.

How it can be done?

David Arenburg
  • 91,361
  • 17
  • 137
  • 196
bogdan.narcis
  • 151
  • 2
  • 7
  • 1
    When you have a string that is the name of an object, you can use `get()` to access the object. (e.g., `x = 5; a = "x"; get(a)`). You'd be better off putting your `plmtest` objects in a list. Rather than `plmtest5 = ...` you'd have `plmtest_list[[5]] = ...`, then you can easily access `plmtest_list[[i]]` without messing with `paste` and `get`. This also has the advantage of letting you use `lapply` and `sapply` to do things to all the objects without writing out a loop. – Gregor Thomas Oct 28 '15 at 15:49
  • 2
    that won''t answer your question put instead of using `paste("plmtest,i,sep="")`use `paste0('plmtest',i)` – etienne Oct 28 '15 at 15:51
  • Thank you for your quick response and suggestions, but I still can't figure it out how to put plmtest5 and plmtest6 in a list with loop. I was also considering using the plyr package, but I am still try to learn and understand it. Thank you very much. – bogdan.narcis Oct 28 '15 at 15:57
  • 1
    Replace `print(paste("plmtest",i,sep=""))` in your code with `print(get(paste("plmtest",i,sep="")))`, or to follow etienne's advice, `print(get(paste0("plmtest",i)))`. You just need to add `get()` around the string you construct. – Gregor Thomas Oct 28 '15 at 15:59
  • Thank you very much Etienne and Gregor! That solved my problem! – bogdan.narcis Oct 28 '15 at 16:02

0 Answers0