0

I have a ncol=10, nrow=343 matrix "E" containing letters "a", "b",..,or "f" standing for technologies, where "a" stands for gas. Whenever [row, col]="a" and [row+1,col] !="a" I add resale values for "a" in [row,col] from s_house_gas dim(ncol=10,nrow=10) where the rows are the years like in E. Additionally, resale values for "a" shall be added in E[5,] to account for a resale in the last year. The code below works, however, I want to add the condition that resale values shall not be added when [row+1] =="d" or "e" but I cannot seem to make it work. Maybe I have looked at it for too long but I would really appreciate your support here, I feel like I am very close.

HV <- rep.int(0, ncol(E))
R<- rbind(E,HV) 

i <- 0
for(col in 1:ncol(R)){
  for(row in 1:nrow(R)){
    if(R[row,col] == "a" && row <= 10){    
      i = i+1
      R[row,col] = i
    } else if (R[row,col] != "a" || row == 11){ 
      i = 0 
    } 
  }
}

R <- R[-11,]

Y <- R

for(row in 1:nrow(Y)){
  for(col in 1:ncol(Y)){
    if(Y[row,col] == "a" || Y[row,col] == "b"|| Y[row,col] == "c" || Y[row,col] == "d"|| Y[row,col] == "e"|| Y[row,col] == "f"){
      Y[row,col] = 0
    }
  }
}

Y <- apply(Y, 2,as.numeric) 

L <- rbind(HV,Y,HV)
M <- matrix(0L, nrow = dim(L)[1], ncol = dim(L)[2])
for(col in 1:ncol(L)){
  for (row in 2:nrow(L)) {
    if(L[row,col] == 0 && L[row-1,col] != 0) {
      M[row,col] = row-1-L[row-1,col]
    }
  }
}

I <- M
N <- matrix(0, nrow = dim(I)[1], ncol = dim(I)[2])
i <- 0
j <- 0
for(row in 2:nrow(I)){
  for(col in 1:ncol(I)){
    if(I[row,col] != 0){
      i = L[row-1,col]
      j = M[row,col]
      N[row-1,col] = s_house_gas[i, j]
    }
  }
}


resale_gas <- N[c(-1,-12),]
resale_gas <- matrix(resale_gas, ncol=343, nrow=10)

EDIT

library(xts)
library(stringi)
library(gtools)

t <- c("a","b","c","d","e","f")
E <- t(permutations(6,5, v=t,repeats.allowed=T))
s_house_gas <- 15:11

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] "a"  "a"  "a"  "a"  "a"  "a"  "a"  "a" 
[2,] "a"  "a"  "a"  "a"  "a"  "a"  "a"  "a" 
[3,] "a"  "a"  "a"  "a"  "a"  "a"  "a"  "a" 
[4,] "a"  "a"  "a"  "a"  "a"  "a"  "b"  "b" 
[5,] "a"  "b"  "c"  "d"  "e"  "f"  "a"  "b" 

This is what I tried:

HV <- rep.int(0, ncol(E))
R<- rbind(E,HV) 

i <- 0
for(col in 1:ncol(R)){
  for(row in 1:(nrow(R)-1)){
    if(R[row,col] == "a" && R[row+1,col] != "d" && row <= 10 || R[row,col] == "a" && R[row+1,col] != "e" && row <= 10){    
      i = i+1
      R[row,col] = i
    } else if (R[row,col] != "a" || row == 11){ 
      i = 0 
    } 
  }
}

R <- R[-11,]
Y <- R

for(row in 1:nrow(Y)){
  for(col in 1:ncol(Y)){
    if(Y[row,col] == "a" || Y[row,col] == "b"|| Y[row,col] == "c" || Y[row,col] == "d"|| Y[row,col] == "e"|| Y[row,col] == "f"){
      Y[row,col] = 0
    }
  }
}

Y <- apply(Y, 2,as.numeric) 

L <- rbind(HV,Y,HV)
M <- matrix(0L, nrow = dim(L)[1], ncol = dim(L)[2])
for(col in 1:ncol(L)){
  for (row in 2:nrow(L)) {
    if(L[row,col] == 0 && L[row-1,col] != 0) {
      M[row,col] = row-1-L[row-1,col]
    }
  }
}

I <- M
N <- matrix(0, nrow = dim(I)[1], ncol = dim(I)[2])
i <- 0
j <- 0
for(row in 2:nrow(I)){
  for(col in 1:ncol(I)){
    if(I[row,col] != 0){
      i = L[row-1,col]
      j = M[row,col]
      N[row-1,col] = s_house_gas[i, j]
    }
  }
}


resale_gas <- N[c(-1,-12),]
resale_gas <- matrix(resale_gas, ncol=343, nrow=10)
Rnewbie
  • 1
  • 2
  • 1
    Hi Rnewbie, the error you describe happens whenever you try to select a row or column that is not within the rows or columns of the matrix. Since you're looking at `row+1`, I guess this happens when `row` equals the number of rows, and hence `row+1` is one more. Getting that row is not possible, so R throws an error. You could include a check like `if (row+1 <= nrow(...))` to fix this. – Bas Jul 30 '20 at 06:20
  • 1
    Also, please include a minimal working example that we can use to help you. Right now, we cannot run your code since the first line fails because we don't have matrix `E`. Please provide this matrix (or the `head` thereof) using for example `dput()`, and check if one is able to run your full code in a fresh session. See also [how to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Bas Jul 30 '20 at 06:23
  • Hi Bas, thank you for the quick reply! I guess this should work: t <- c("a","b","c","d","e","f") E <- t(permutations(6,5, v=t,repeats.allowed=T)) s_house_gas <- 15:11 – Rnewbie Jul 30 '20 at 08:13
  • Please make also sure to include all packages, by stating the respective library(...) commands in the beginning or using double colon notation. Where does permutations() come from? Without some examplary data, as stated above, you probably won't get any help.u – mabreitling Jul 30 '20 at 08:58
  • Thank you for your response, @mabreitling. I edited the code and included a part of the E matrix. I hope this helps. – Rnewbie Jul 30 '20 at 13:09

0 Answers0