'Putting values into a matrix only for even or odd numbered rows
I am trying to specifically insert certain characters (the alphabet) into a matrix in R. Here is what I have so far.
M2 = matrix(nrow=100, ncol=26)
for (i in (1:nrow(M2)))
{
for (j in (1:ncol(M2)))
{
if (i==1)
M2[i, ] <- LETTERS
}
{
if (i==2)
M2[i, ] <- rev(LETTERS)
}
}
M2
## [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
## [1,] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M"
## [2,] "Z" "Y" "X" "W" "V" "U" "T" "S" "R" "Q" "P" "O" "N"
## [3,] NA NA NA NA NA NA NA NA NA NA NA NA NA
## [4,] NA NA NA NA NA NA NA NA NA NA NA NA NA
## [5,] NA NA NA NA NA NA NA NA NA NA NA NA NA
## [6,] NA NA NA NA NA NA NA NA NA NA NA NA NA
## [7,] NA NA NA NA NA NA NA NA NA NA NA NA NA
## [8,] NA NA NA NA NA NA NA NA NA NA NA NA NA
## [9,] NA NA NA NA NA NA NA NA NA NA NA NA NA
## [10,] NA NA NA NA NA NA NA NA NA NA NA NA NA
This works quite well so far, but I need every odd numbered row (1, 3, 5, etc. up to 100) to have the letters and every even row up to 100 to have the rev(LETTERS). What would I need to replace the if== section with to achieve this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
