AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
If code to stop loop in r4/8/2024 ![]() ![]() ![]() This code says we’ll loop 50 times( 1:50). So, we set up an empty vector to add stuff to later (note that this isn’t the most speed efficient way to do this, but it’s fairly fool-proof). R doesn’t like being told to operate on a vector that doesn’t exist yet. If the creation of a new vector is the goal, first you have to set up a vector to store things in prior to running the loop. I’m going to set up a loop to square every element of my dataset, foo, which contains the odd integers from 1 to 100 (keep in mind that vectorizing would be faster for my trivial example – see below). ![]() Use the for loop if you want to do the same task a specific number of times. ?'for' will get you the help page but it is by no means exhaustive.) So, at the request of Sam, a faithful reader of the Paleocave blog, I’m going to throw my hat into the ring and brace myself for the potential onslaught of internet troll wrath. In fact, even searching for help within R is not easy and not even that helpful when successful ( ?for won’t get you anywhere. I was astounded by the lack of useful posts when I googled “for loops in R” (the top return linked to a page that did not exist). And, to top it off, good help is hard to find. There may be no R topic that is more controversial than the humble for-loop. ![]()
0 Comments
Read More
Leave a Reply. |