Problem with populating matrix with if statement

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. 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.

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

# Example:
     [,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" 

# Where I would want the table to look sth like this:
     [,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"  "13"  "13" 
[4,] "a"  "12"  "12"  "a"  "a"  "12"  "b"  "b" 
[5,] "11"  "b"  "c"  "d"  "e"  "f"  "11"  "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)

Here's a tidyverse solution:

library(tidyverse)

t <- c("a", "b", "c", "d", "e", "f")
# smaller so can see it
E <- tribble(
  ~x1, ~x2, ~x3, ~x4, ~x5, ~x6, ~x7, ~x8,
  "a", "a", "a", "a", "a", "a", "a", "a",
  "a", "a", "a", "a", "a", "a", "a", "a",
  "a", "a", "a", "a", "a", "a", "a", "a",
  "a", "a", "a", "a", "a", "a", "b", "b",
  "a", "b", "c", "d", "e", "f", "a", "b"
) %>%
  mutate(rowname = row_number()) %>%
  add_row(tribble(
    ~x1, ~x2, ~x3, ~x4, ~x5, ~x6, ~x7, ~x8,
    "a", "a", "a", "a", "a", "a", "a", "a"
  )) # dummy row for lead to not NA
E
s_house_lookup <- enframe(s_house_gas <- 15:11, name = "rowname")

joined <- left_join(E, s_house_lookup)

(results <- joined %>%
  mutate(across(
    where(is.character),
    ~ if_else(. == "a" & (! lead(.) %in% c("a","d","e")), as.character(value), .)
  )) %>%
  slice(-nrow(E)) # remove dummy tow
)
  x1    x2    x3    x4    x5    x6    x7    x8    rowname value
  <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr>   <int> <int>
1 a     a     a     a     a     a     a     a           1    15
2 a     a     a     a     a     a     a     a           2    14
3 a     a     a     a     a     a     13    13          3    13
4 a     12    12    a     a     12    b     b           4    12
5 a     b     c     d     e     f     a     b           5    11

your example does have all a's on the 5th line as 11, is this because there is a 6th line, where they are different and not moving to d or e ? or some other principle ?

hey @nirgrahamuk, thank you very much for your help! Unfortunately, I would need the values in E[5, 1] and E[5, 7]. I am really new to R and your solution is very difficult for me to understand (I have not even ever worked with dplyr before). I also see the table immediately occurs in the output but I would need the matrix to further work with it. When I View(E), the matrix is empty. Could you give me a hint on how to access the matrix with the values please? I really appreciate the effort you put into this!

what principle explains why coordinate [5,1] should have 11 in it ?
can it be explained in the form of an additional rule ?

the table is stored in results it can easily be manipulated to a matrix

results_matrix <- select(results,
                         -rowname,-value) %>% 
                    as.matrix()

colnames(results_matrix) <- NULL

results_matrix

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.