Filling a matrix after doing the row and column comparison in R

May I ask you how can I compare a table (which has these attributes: initial machine name, destination machine name and incremental numeric ID) and an empty array (which has as attributes: arrival machine name in the columns and the target machine name start in lines).

If there is this correspondence between the departure and the arrival, then I have to fill with the incremental id value inside the array

for (i in nrow(rownames(tabellaMatrice))) {
  for (j in ncol(colnames(tabellaMatrice))) {
    for (r in nrow(tab2$Da)) {
      ifelse(tab2$Da[r]==rownames(tabellaMatrice)[i],
             for (o in nrow(tab2$A)) {
               ifelse(tab2$A[o]==colnames(tabellaMatrice),
                      for (p in nrow(tab2```
Numero incrementale`)) {
                        tabellaMatrice[i,j]<-tab2```
Numero incrementale`[p]
                        
                      }
                      ,"")
               
             },"")
      
    }
    
  }
  
}

I'm trying to do this with for loops

I expect that inside the array is an incremental id.
image1

Is this the sort of thing you are trying to do?

tabellaMatrice <- data.frame(Num_Inc = c(1,1,2,2,3,3,3,4,4),
                             Da = c("A51","B05","A51","C09","M23",
                                    "X19","C09","B07","C10"),
                             A = c("B07","C10","A51","A51","X19",
                                   "C09","X19","B05","M23"))
tabellaMatrice
#>   Num_Inc  Da   A
#> 1       1 A51 B07
#> 2       1 B05 C10
#> 3       2 A51 A51
#> 4       2 C09 A51
#> 5       3 M23 X19
#> 6       3 X19 C09
#> 7       3 C09 X19
#> 8       4 B07 B05
#> 9       4 C10 M23

tab2 <- matrix(NA,nrow = 7, ncol = 7)
dimnames(tab2) <- list(c("A51","B05","B07","C09","C10","M23","X19"),
                       c("A51","B05","B07","C09","C10","M23","X19"))
tab2
#>     A51 B05 B07 C09 C10 M23 X19
#> A51  NA  NA  NA  NA  NA  NA  NA
#> B05  NA  NA  NA  NA  NA  NA  NA
#> B07  NA  NA  NA  NA  NA  NA  NA
#> C09  NA  NA  NA  NA  NA  NA  NA
#> C10  NA  NA  NA  NA  NA  NA  NA
#> M23  NA  NA  NA  NA  NA  NA  NA
#> X19  NA  NA  NA  NA  NA  NA  NA

for(i in 1:nrow(tabellaMatrice)) {
  tab2[tabellaMatrice[i,"Da"], tabellaMatrice[i,"A"]] <- tabellaMatrice[i, "Num_Inc"]
}

tab2
#>     A51 B05 B07 C09 C10 M23 X19
#> A51   2  NA   1  NA  NA  NA  NA
#> B05  NA  NA  NA  NA   1  NA  NA
#> B07  NA   4  NA  NA  NA  NA  NA
#> C09   2  NA  NA  NA  NA  NA   3
#> C10  NA  NA  NA  NA  NA   4  NA
#> M23  NA  NA  NA  NA  NA  NA   3
#> X19  NA  NA  NA   3  NA  NA  NA

Created on 2022-12-09 with reprex v2.0.2

It's working!!!! But I have one question, How do I separate the values inside the box if there were some duplicates? I already tried with the function "paste", but it doesn't work.
Thanks in advance for your time!

I think paste() will work with some adjustments. In this example, one cell has two entries.

tabellaMatrice <- data.frame(Num_Inc = c(1,1,2,2,3,3,3,4,4,4),
                             Da = c("A51","B05","A51","C09","M23",
                                    "X19","C09","B07","C10","B05"),
                             A = c("B07","C10","A51","A51","X19",
                                   "C09","X19","B05","M23","C10"))
tabellaMatrice
#>    Num_Inc  Da   A
#> 1        1 A51 B07
#> 2        1 B05 C10
#> 3        2 A51 A51
#> 4        2 C09 A51
#> 5        3 M23 X19
#> 6        3 X19 C09
#> 7        3 C09 X19
#> 8        4 B07 B05
#> 9        4 C10 M23
#> 10       4 B05 C10

tab2 <- matrix("",nrow = 7, ncol = 7)
dimnames(tab2) <- list(c("A51","B05","B07","C09","C10","M23","X19"),
                       c("A51","B05","B07","C09","C10","M23","X19"))
tab2
#>     A51 B05 B07 C09 C10 M23 X19
#> A51 ""  ""  ""  ""  ""  ""  "" 
#> B05 ""  ""  ""  ""  ""  ""  "" 
#> B07 ""  ""  ""  ""  ""  ""  "" 
#> C09 ""  ""  ""  ""  ""  ""  "" 
#> C10 ""  ""  ""  ""  ""  ""  "" 
#> M23 ""  ""  ""  ""  ""  ""  "" 
#> X19 ""  ""  ""  ""  ""  ""  ""

for(i in 1:nrow(tabellaMatrice)) {
  tab2[tabellaMatrice[i,"Da"], tabellaMatrice[i,"A"]] <- 
    trimws(paste(tab2[tabellaMatrice[i,"Da"], tabellaMatrice[i,"A"]],tabellaMatrice[i, "Num_Inc"]))
}

tab2
#>     A51 B05 B07 C09 C10   M23 X19
#> A51 "2" ""  "1" ""  ""    ""  "" 
#> B05 ""  ""  ""  ""  "1 4" ""  "" 
#> B07 ""  "4" ""  ""  ""    ""  "" 
#> C09 "2" ""  ""  ""  ""    ""  "3"
#> C10 ""  ""  ""  ""  ""    "4" "" 
#> M23 ""  ""  ""  ""  ""    ""  "3"
#> X19 ""  ""  ""  "3" ""    ""  ""

Created on 2022-12-09 with reprex v2.0.2

1 Like

Thank you very much for your time dedicated to this problem. Really, thanks again!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.