Loops in R: Taking code out to R Studio

Hi, Everyone!

I was working through this lessons on Loops in R: https://www.codecademy.com/paths/analyze-data-with-r/tracks/introduction-to-programming-in-r/modules/intro-to-programming-in-r/articles/loops-in-r

The second Code Challenge (beginning, “Edit the code block to print the matrix elements column by column…” doesn’t have an output window. I wanted to verify that the changes I was making looked how I expected, so I took the code out to RStudio.

When I copy and paste the code into RStudio, here’s the output. I apologize is this is an obvious answer, but I’m brand new to both R and RStudio. Thank you!

Error: attempt to use zero-length variable name
> # define the matrix
> my_matrix <- matrix(c(12, 11, 24, 8, 2, 3, 9, 4, 12, 13, 25, 7, 23, 10, 19), nrow = 3, ncol = 5)
> ```
Error: attempt to use zero-length variable name
> 
> ```{r}
Error: attempt to use zero-length variable name
> # modify the for statements:
> for (col in 1:ncol(my_matrix)) {
+   for (row in 1:nrow(my_matrix)) {
+     print(paste("Column", col, "Row", row, ":", my_matrix[col, row]))
+   }
+ }
[1] "Column 1 Row 1 : 12"
[1] "Column 1 Row 2 : 8"
[1] "Column 1 Row 3 : 9"
[1] "Column 2 Row 1 : 11"
[1] "Column 2 Row 2 : 2"
[1] "Column 2 Row 3 : 4"
[1] "Column 3 Row 1 : 24"
[1] "Column 3 Row 2 : 3"
[1] "Column 3 Row 3 : 12"
Error in my_matrix[col, row] : subscript out of bounds
> ```
Error: attempt to use zero-length variable name