How to Write R

As one writes R

The R-users whose questions or comments helped me to write "R for beginners". This is how to spell the first one for cycle in Ra We will take a look at how to create a Basics for Loops in Ru. It is intended for novices, and if you are not yet acquainted with the basics of the Ru-Sprache, we suggest you take a look at this first-run intro.

Conceptionally, a cycle is a way of repeating a series of statements under certain circumstances. Allows you to automatize parts of your codes that need to be repeated. However, as a novice in playing it is good to have a fundamental grasp of looping and how to use it.

Let us return to the conceptional significance of a series. Year is[ year], where[year] is 2010, 2011, until 2015. "Year is 2010 " "Year is 2011" "Year is 2012" "Year is 2013" "Year is 2014" "Year is 2015" You can see this immediately: You always reiterate the same cipher.

If this is the case, you can use a for cycle in the for to automatize the recurring part in R: "Year is 2010 " "Year is 2011" "Year is 2012" "Year is 2013" "Year is 2014" "Year is 2015" The best way to comprehend what is going on in the for-cycle is to read it as follows:

"Each year in order c(2010,2011,2012,2013,2014,2015), run the following code: check print(paste("The year is", year))". When the for statement has run the source for each year in the array, the cycle will stop and proceed to the first statement after the cycle group. Using a for loops means you only have to record your source codes once (instead of six times).

Then the for cycle executes the instruction once for each value provided (the different years we specify) and set the value of the tag (in this case the year). They can even further facilitate the code: c(2010,2011,2012,2012,2013,2014,2015) can also be typed as 2010:2015; this generates exactly the same order: "Year is 2010 " "Year is 2011" "Year is 2012" "Year is 2013" "Year is 2014" "Year is 2015" As a final remark on the for cycle in R: In this case we used the for-cycle, but in fact any variables could be used here.

You could have used i, for example, a frequently used loop variables that represents index: "Year is 2010" "Year is 2011" "Year is 2012" "Year is 2013" "Year is 2014" "Year is 2015". You can still name the variables, but it is easier to understand if you use descriptive name.

This is how your bow would look: Note the introductory part of the next instruction. Let us investigate the significance of this message by going through this cycle together: If i is between 1 and 10, we step into the cycle and if not, the cycle will stop. If we do join the cycle, we have to verify if the value of i is odd.

When the value of i has a rest of zero when it is split by 2 (therefore we use the module operands %%), we do not issue the if instruction, perform the printing operation and return. If the rest is not zero, the if-command is evaluated to the value TRUE and we enter the condition.

This is the next instruction that causes a 1:10 return path to be made to e, and ignores the following statements (i.e. the print(i)). You have learnt about the for cycle in Ru. While the use of cycles in Ru should generally be excluded, it is still useful to have this in your skill set.

This will help you learn the basic concepts and a simple to program and interpret prototype loops.

Mehr zum Thema