Home > front end >  multiply vector by variable to create a matrix with for loop
multiply vector by variable to create a matrix with for loop

Time:10-23

I am trying to create a matrix that will demonstrate a industry production projected in the next x years.

First I have created a for loop to create the columns names with: (10 years for example)

column.name <- vector()
for (i in 1:10) {
    object <- paste('ano.',i)
    column.name[i] <- object
}

then I saved my products in a vector

products <- c('a','b','c',)

In this case I have a machine that will be used for all three products. So, I have a participacion vector, indicating how much of each product will be produced considering the machine capacity (4800).

participation<- c(0.60,0.20,0.20)
amount <- 4800

finaly i tried to multiplicate 'participation' by amout with a for loop

prod <- matrix(length(products):length(column.name))
for (j in 1:length(column.name)) {
  for (i in 1:length(product)) {
    dado <- participation[i]*amount
    prod[i,j] <- dado
  }
  return(prod)
}

theoretically I would get a matrix with production values for each year by product, which would be used to calculate the income. However, all I got was a 1x1 matrix with null value.

Could you help me?

CodePudding user response:

You were close, here is a working version of your example:

column.name <- vector()
for (i in 1:10) {
  object <- paste('ano.',i)
  column.name[i] <- object
}

products <- c('a','b','c')

participation<- c(0.60,0.20,0.20)
amount <- 4800

prod <- matrix(data = NA, nrow = length(products), ncol = length(column.name))

for (j in 1:length(column.name)) {
  for (i in 1:length(products)) {
    dado <- participation[i]*amount
    prod[i,j] <- dado
  }
}

A few things to consider:

  • matrix() creates a new matrix with parameters: data, nrow, ncol (in that order). You should either specify each parameter in order or explicitly name parameters (ie. nrow=) otherwise your first parameter gets interpreted as the data parameter.
  • Careful around typos! You had length(product) rather than length(products), you had an extra comma in products, etc.
  • A deeper question, outside the scope of your specific ask, is whether you should be using matrices versus data.frames for your analysis.

CodePudding user response:

Are you looking for something like this?

column.name <- paste0('ano.', 1:10)
products <- c('a','b','c')
participation<- c(0.60,0.20,0.20)
amount <- 4800

mat <- matrix(NA, 
              nrow = 3, 
              ncol = 10,
              dimnames = list(product = products,
                              ano = column.name))


mat[] <- participation * amount

mat
#>        ano
#> product ano.1 ano.2 ano.3 ano.4 ano.5 ano.6 ano.7 ano.8 ano.9 ano.10
#>       a  2880  2880  2880  2880  2880  2880  2880  2880  2880   2880
#>       b   960   960   960   960   960   960   960   960   960    960
#>       c   960   960   960   960   960   960   960   960   960    960

Created on 2021-10-22 by the reprex package (v2.0.0)

Another option is to use the outer function, and name the rows and columns of the matrix after it is produced

participation<- c(0.60,0.20,0.20)
amount <- rep(4800, 10)

mat <- outer(participation, amount)

colnames(mat) <- paste0('ano.', 1:10)
row.names(mat) <- c('a','b','c')

mat
#>   ano.1 ano.2 ano.3 ano.4 ano.5 ano.6 ano.7 ano.8 ano.9 ano.10
#> a  2880  2880  2880  2880  2880  2880  2880  2880  2880   2880
#> b   960   960   960   960   960   960   960   960   960    960
#> c   960   960   960   960   960   960   960   960   960    960

Created on 2021-10-22 by the reprex package (v2.0.0)

  • Related