This is a modification of "DNA Sequence Statistics (1)" from Avril Coghlan's A little book of R for bioinformatics.. Almost all of text and code was originally written by Dr. Coghlan and distributed under the Creative Commons 3.0 license.
curly brackets
for
In R, just as in programming languages such as Python, it is possible to write a for loop to carry out the same command several times. For example, if we want to print out the square of each number between 1 and 4, we can write the following for loop:
for (i in 1:4) { print (i*i) }
In the for loop above, the variable i is a counter or index for the number of cycles through the loop. In the first cycle through the loop, the value of i is 1, and so i * i = 1 is printed out. In the second cycle through the loop, the value of i is 2, and so i * i = 4 is printed out. In the third cycle through the loop, the value of i is 3, and so i * i = 9 is printed out. The loop continues until the value of i is 4.
Note that the commands that are to be carried out at each cycle of the for loop must be enclosed within curly brackets (“{” and “}”).
You can also give a for loop a vector of numbers containing the values that you want the counter i to take in subsequent cycles. For example, you can make a vector avector containing the numbers 2, 9, 100, and 133, and write a for loop to print out the square of each number in vector avector:
avector <- c(1, 2, 3, 4) for (i in avector) { print (i*i) }
The results should be the same as before.
How can we use a for loop to print out the square of every second number between, say, 1 and 10? The answer is to use the seq() function with "by = 2" to tell the for loop to take every second number between 1 and 10:
for (i in seq(1, 10, by = 2)) { print (i*i) }
In the first cycle of this loop, the value of i is 1, and so i * i = 1 is printed out. In the second cycle through the loop, the value of i is 3, and so i * i = 9 is printed out. The loop continues until the value of i is 9. In the fifth cycle through the loop, the value of i is 9, and so i * i = 81 is printed out.
We have been using built-in functions such as mean(), length(), print(), plot(), etc. We can also create our own functions in R to do calculations that you want to carry out very often on different input data sets. For example, we can create a function to calculate the value of 20 plus the square of some input number:
myfunction <- function(x) { output <- (20 + (x*x)) return(output) }
This function will calculate the square of a number (x), and then add 20 to that value. It stores this in a tempory object alled output. The return() statement returns the calculated value. Once you have typed in this function, the function is then available for use. For example, we can use the function for different input numbers (eg. 10, 25):
myfunction(10) myfunction(25)
You can view the code that makes up a function by typing its name (without any parentheses). For example, we can try this by typing “myfunction”:
myfunction
When you are typing R, if you want to, you can write comments by writing the comment text after the “#” sign. This can be useful if you want to write some R commands that other people need to read and understand. R will ignore the comments when it is executing the commands. For example, you may want to write a comment to explain what the function log10() does:
x <- 100 log10(x) # Finds the log to the base 10 of variable x.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.