
Vector code for snippets with for loop and if else code#

The code editor I will use to showcase snippets is Visual Studio Code (VS Code), the most popular editor of the day.Īlso, there are some "text expander" apps that allow you to use snippets globally (across all apps). Most code editors support snippets out of the box. Utter a short phrase (type a prefix), wave your wand (press Enter or Tab), and presto! A wonderful event unfolds in front of you. You need to first create minus as above and index into it for the assignment like this minus <- newValue.Snippets can add a touch of magic to your editor. The warning was saying that it was just using the first number in your start and finish and giving you 30:50.įinally, you were constantly over writing your value of minus rather than adding to it. For example, when i = "Blue", you were trying to make a sequence starting at both 30 and 27 and ending at both 50 and 85. The reason you were getting those warnings was a combination of this problem and the last one. For example, when i = "Yellow" it returns 99:25 or 99, 98, 97. Second when you make the statement abund:abund you are not indexing the columns C2 C3 and C4 you are making a sequence starting at the value in C2 and ending at the value in C4. Blue will give you 1 and 9, since both those rows Species = "Blue"). As a result, then you index using abund, you may return multiple rows (ex. In each round, the value of i is being set to the next value in abund$Species, so first it is Blue then Black then Yellow, etc. First, your first for loop isn't doing what you think.

Minus <- rowSums(abund)Īlso, for what it is worth there are quite a few problems with your original code. This is probably better done without any loops.

Thank you for your time and help with this. Which returns this: There were 12 warnings (use warnings() to see them) Minus <- ifelse(i = j, sum(abund[abund$Species = i, This is my code so far: # Use for loop to create vector of sums for select species or 0 for species not selected I'm having trouble with my code and hope it's just a small matter of defining a range, but I'm not sure. If the species in abund does not match the species in color then add a 0 to the new vector minus. So, if the Species in abund matches the species in color then add columns C2 through C4 together in a new vector minus.

I would like to add together some of abund’s columns but only if they match the correct species I’ve specified in the vector colors. This is my dataframe abund: Species Total C1 C2 C3 C4 I'm having a problem with nested for loops and ifelse statements.
