Home > OS >  How to repeat a function n times?
How to repeat a function n times?

Time:10-12

I was able to write a function in r to "shift" a column of a matrix over to the right by one:

shift <- function(disc){
  mat <- matrix(nrow = 4, ncol = 12)
  mat[,1] <- disc[,12]
  for(i in 1:11){
    mat[,i 1] <- disc[,i] 
  }
  return(mat)
}

So to see how that works:

> disc0
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]    2    5   10    7   16    8    7    8    8     3     4    12
[2,]    3    3   14   14   21   21    9    9    4     4     6     6
[3,]    8    9   10   11   12   13   14   15    4     5     6     7
[4,]   14   11   14   14   11   14   11   14   11    11    14    11
> shift(disc0)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]   12    2    5   10    7   16    8    7    8     8     3     4
[2,]    6    3    3   14   14   21   21    9    9     4     4     6
[3,]    7    8    9   10   11   12   13   14   15     4     5     6
[4,]   11   14   11   14   14   11   14   11   14    11    11    14

What if I wanted to shift over 3 times, for example? I could do this manually:

> x <- disc0
> x <- shift(x)
> x <- shift(x)
> x <- shift(x)
> x
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,]    3    4   12    2    5   10    7   16    8     7     8     8
[2,]    4    6    6    3    3   14   14   21   21     9     9     4
[3,]    5    6    7    8    9   10   11   12   13    14    15     4
[4,]   11   14   11   14   11   14   14   11   14    11    14    11

So now the original first column (2,3,8,14) is now in the 4th column.

But how can I automate this? I want to write a function that will repeat my shift function n times. Thanks in advance

CodePudding user response:

You could write a function that takes in the shift parameter:

shift <- function(x, num = 1){
  n <- ncol(x)
  x[, c((n - num  1):n, 1:(n - num))] 
}

mat
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    4    5    6    7    8
[2,]    1    2    3    4    5    6    7    8
[3,]    1    2    3    4    5    6    7    8
[4,]    1    2    3    4    5    6    7    8

shift(mat)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    8    1    2    3    4    5    6    7
[2,]    8    1    2    3    4    5    6    7
[3,]    8    1    2    3    4    5    6    7
[4,]    8    1    2    3    4    5    6    7
shift(mat,2)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    7    8    1    2    3    4    5    6
[2,]    7    8    1    2    3    4    5    6
[3,]    7    8    1    2    3    4    5    6
[4,]    7    8    1    2    3    4    5    6
shift(mat,3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    6    7    8    1    2    3    4    5
[2,]    6    7    8    1    2    3    4    5
[3,]    6    7    8    1    2    3    4    5
[4,]    6    7    8    1    2    3    4    5

CodePudding user response:

You may use a for loop -

n <- 3
for(i in seq_len(n)) {
  disc0 <- shift(disc0)  
}
disc0

#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#[1,]    3    4   12    2    5   10    7   16    8     7     8     8
#[2,]    4    6    6    3    3   14   14   21   21     9     9     4
#[3,]    5    6    7    8    9   10   11   12   13    14    15     4
#[4,]   11   14   11   14   11   14   14   11   14    11    14    11
  • Related