'R: for-loop over matrix returns only last values [duplicate]

So I have a matrix filled with random numbers: mat_base = matrix(sample(1:20, 100, replace = TRUE), nrow=10, ncol=10) and another matrix mat_table = matrix(,100,3) which has the column names: colnames(mat_table) <- c("row", "col", "value").

I want to put all values and their locations (row and column numbers) from mat_base to mat_table and I have already succeeded doing so with the locations. But for the values for each cell mat_table only returns the values from the last column of mat_base.

This is the code I have so far:

for (j in 1:nrow(mat_base)) {
  for (k in 1:ncol(mat_base)){ 
    #2 nested for-loops to go through mat_base
    
    mat_table[,1] <- row(mat_base)
    #the function "row" returns a matrix of row numbers
    #put row numbers in the first column of mat_table
    
    mat_table[,2] <- col(mat_base)
    #the function "col" returns a matrix of column numbers
    #put column numbers in the second column of mat_table
    
    mat_table[,3] <- mat_base[,k]
    #get current value of mat_base and put it into mat_table
    #put values in the third column of mat_table

  }
  
}

mat_table
#show mat_table in the console

I know that I might not need the outer for-loop with j because the result is the same without it (I'm not using j anyways) but I wasn't sure so I left it in for now.

This is the reult I am getting with this code:

> mat_table
       row col value
  [1,]   1   1     4
  [2,]   2   1    11
  [3,]   3   1    13
  [4,]   4   1    13
  [5,]   5   1     2
  [6,]   6   1    17
  [7,]   7   1     7
  [8,]   8   1    14
  [9,]   9   1    19
 [10,]  10   1     3
 [11,]   1   2     4
 [12,]   2   2    11
 [13,]   3   2    13
 [14,]   4   2    13
 [15,]   5   2     2
 [16,]   6   2    17
 [17,]   7   2     7
 [18,]   8   2    14
 [19,]   9   2    19
 [20,]  10   2     3
 [21,]   1   3     4
 [22,]   2   3    11
 [23,]   3   3    13
...

As you can see in the third column of mat_table only the last column of mat_base is repeated over and over again. How can I get all values from mat_base into mat_table?

Edit/Answer: Okay I found out that by using loops I made my life a lot harder than it had to be... This is how I solved this problem in the end:

mat_base = matrix(sample(1:20, 100, replace = TRUE), nrow=10, ncol=10) 
#my data

mat_table = matrix(,100,3)
#create an empty matrix with 100 rows and 3 columns
colnames(mat_table) <- c("row", "col", "value")
#change the column names 

mat_table <- cbind(c(row(mat_base)), c(col(mat_base)), c(mat_base))
#put row numbers, column numbers and values from mat_base into mat_table

mat_table
#show mat_table in console

Edit 2: But if you want to do it the way I was trying initially, check the answer from @Mohanasundaram!



Solution 1:[1]

An alternate way of getting the desired table is this.

mat_base = matrix(sample(1:20, 100, replace = TRUE), nrow=10, ncol=10)

mat_table <- data.frame(row = rep(1:nrow(mat_base), by = ncol(mat_base)),
                        col = rep(1:ncol(mat_base), each = nrow(mat_base)),
                        value = c(mat_base))

For your original method, this is the solution. You have to filter the matrix by row names and columns names and then assign the value. However, this is not an efficient way.

mat_base = matrix(sample(1:20, 100, replace = TRUE), nrow=10, ncol=10)
mat_table = matrix(,100,3)
colnames(mat_table) <- c("row", "col", "value")

for (j in 1:nrow(mat_base)) {
  for (k in 1:ncol(mat_base)){ 
    #2 nested for-loops to go through mat_base
    
    mat_table[,1] <- row(mat_base)
    #the function "row" returns a matrix of row numbers
    #put row numbers in the first column of mat_table
    
    mat_table[,2] <- col(mat_base)
    #the function "col" returns a matrix of column numbers
    #put column numbers in the second column of mat_table
    
    mat_table[mat_table[,"row"] == j & mat_table[,"col"] == k,3] <- mat_base[j,k]
    #get current value of mat_base and put it into mat_table
    #put values in the third column of mat_table
    
  }
  
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1