0

I'd like to use loop function in R with following code:

pc01 <- qcc(matrix(cbind(dta02[1:length(dta02)-1], dta02[2:length(dta02)]), ncol = 2), type = "R")
pc02 <- qcc(matrix(cbind(dta02[1:length(dta02)-1], dta02[2:length(dta02)]), ncol = 2), type = "R")
pc03 <- qcc(matrix(cbind(dta03[1:length(dta03)-1], dta03[2:length(dta03)]), ncol = 2), type = "R")

In order to make it easily, I used the 'function'

myqccF <- function(n)
{
 qcc(matrix(cbind(n[1:length(n)-1], n[2:length(n)]), ncol = 2), type = "R")
}

and then, how do I make the loop using the function 'myqccF' like the following code?

qc01 <- myqccF(dta01)
qc02 <- myqccF(dta02)
qc03 <- myqccF(dta03)
Jaap
  • 81,064
  • 34
  • 182
  • 193
jhyeon
  • 456
  • 4
  • 14
  • 1
    you can use assign function in the for-loop with paste0. Maybe like this: for (i in 1:3){ temp <- paste0("dta0",i) assign(paste0("qcc0",i), myqccF(temp)) } but you might need to add 'return' function at the end of your function defining. – Mons2us Apr 23 '17 at 07:15

1 Answers1

0

You still have a loop, now it's a loop with your function in it.

for (i in 1:3) assign(paste0("qc0", i), myqccF(get(paste0("qc0", i))))
Gladwell
  • 328
  • 3
  • 10