using for loop to create new row

Hi everyone, I am trying to create a new row in a matrix with the following pattern:
“PR_1972”, “CL_1972”, “Status_1972”, "PR_1973”, “CL_1973”, “Status_1973”,
etc, all the way until 2016. I want to perform some type of loop but I don't know how to code this.

It is not clear from your question if you want to add 1 row with 135 columns or 45 rows with 3 columns each.

Anyway if M is a matrix then r M[i,j] is the i-th row and the j-th column of the matrix.
You can loop by r i | j or even both (double loop).

for( i in 1: 2016-1973+1){
  for(j in 1:3){
    do something
}
}

You can use a for loop as shown by @JonesYaniv but you can also use the paste() function and rbind(). I invented two versions of a matrix named MAT that contains just the letters A - X and appended four years of your PR, CL, Status pattern.

NewVals <- paste(c("PR","CL","Status"), rep(1972:1975, each = 3),sep="_")
NewVals
#>  [1] "PR_1972"     "CL_1972"     "Status_1972" "PR_1973"     "CL_1973"    
#>  [6] "Status_1973" "PR_1974"     "CL_1974"     "Status_1974" "PR_1975"    
#> [11] "CL_1975"     "Status_1975"

#matrix with 2 rows and 12 columns
MAT <- matrix(LETTERS[1:24], nrow = 2)
MAT
#>      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
#> [1,] "A"  "C"  "E"  "G"  "I"  "K"  "M"  "O"  "Q"  "S"   "U"   "W"  
#> [2,] "B"  "D"  "F"  "H"  "J"  "L"  "N"  "P"  "R"  "T"   "V"   "X"
MAT <- rbind(MAT, NewVals)
MAT
#>         [,1]      [,2]      [,3]          [,4]      [,5]      [,6]         
#>         "A"       "C"       "E"           "G"       "I"       "K"          
#>         "B"       "D"       "F"           "H"       "J"       "L"          
#> NewVals "PR_1972" "CL_1972" "Status_1972" "PR_1973" "CL_1973" "Status_1973"
#>         [,7]      [,8]      [,9]          [,10]     [,11]     [,12]        
#>         "M"       "O"       "Q"           "S"       "U"       "W"          
#>         "N"       "P"       "R"           "T"       "V"       "X"          
#> NewVals "PR_1974" "CL_1974" "Status_1974" "PR_1975" "CL_1975" "Status_1975"

#matrix with 3 columns and 8 rows
MAT <- matrix(LETTERS[1:24], ncol = 3)
MAT
#>      [,1] [,2] [,3]
#> [1,] "A"  "I"  "Q" 
#> [2,] "B"  "J"  "R" 
#> [3,] "C"  "K"  "S" 
#> [4,] "D"  "L"  "T" 
#> [5,] "E"  "M"  "U" 
#> [6,] "F"  "N"  "V" 
#> [7,] "G"  "O"  "W" 
#> [8,] "H"  "P"  "X"
MAT2 <- matrix(NewVals, ncol = 3, byrow = TRUE)
MAT2
#>      [,1]      [,2]      [,3]         
#> [1,] "PR_1972" "CL_1972" "Status_1972"
#> [2,] "PR_1973" "CL_1973" "Status_1973"
#> [3,] "PR_1974" "CL_1974" "Status_1974"
#> [4,] "PR_1975" "CL_1975" "Status_1975"
MAT <- rbind(MAT, MAT2)
MAT
#>       [,1]      [,2]      [,3]         
#>  [1,] "A"       "I"       "Q"          
#>  [2,] "B"       "J"       "R"          
#>  [3,] "C"       "K"       "S"          
#>  [4,] "D"       "L"       "T"          
#>  [5,] "E"       "M"       "U"          
#>  [6,] "F"       "N"       "V"          
#>  [7,] "G"       "O"       "W"          
#>  [8,] "H"       "P"       "X"          
#>  [9,] "PR_1972" "CL_1972" "Status_1972"
#> [10,] "PR_1973" "CL_1973" "Status_1973"
#> [11,] "PR_1974" "CL_1974" "Status_1974"
#> [12,] "PR_1975" "CL_1975" "Status_1975"

Created on 2023-09-22 with reprex v2.0.2

This topic was automatically closed 42 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.