Search code examples
arraysrfor-loopcbind

array in R number of items to replace is not a multiple of replacement length


I would like to ask, how to set a value in array using looping. say like this

a<-3
b<-4
for( i in 1:5)
{
  x[i] <- cbind(a*i, b*i)
}

but i always get error saying : In x[i] <- cbind(a * i, b * i) : number of items to replace is not a multiple of replacement length. I used "paste" but seems it's not the solution. What is the problem ? If it were solved, can I get the value by using ; for example x[2][,2] to get the value of b * 2 ? thank you


Solution

  • You can do it this way :

    a <- 3
    b <- 4
    i <- 1:5
    x <- cbind(a*i, b*i)