In this recipe, we will first define a square matrix and then write a nested for loop to print only those values where I J, namely, the values in the matrix placed in (1,1), (2,2), and so on. If you have further questions, please tell me about it in the comments section. Nested loops in R We can nest loops, as well as if statements, to perform some more complicated tasks. In summary: You learned in this article how to store loop results in a vector in R. However, if you want to save many items, each as a separate file, you have to use a loop and a paste() command It is easy to save the objects you have stored in the R Console. In addition, you could read some of the other articles on my website. You can easily save one or more objects to disk using save() or save.image(). I’m showing the examples of this article in the video: Would you like to learn more about vectors and loops? Then you might have a look at the following video of my YouTube channel. Note that we could apply the same type of R code within a repeat-loop or within a while-loop as well. My_vec # Show final vector # "A" "B" "C" "D" "E"Īs you can see based on the previous output of the RStudio console, the previous for-loop has created a vector consisting of five character elements.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |