WB-ILIAS | Weiterbildung und offene Bildungsressourcen

R

Simple input

Most of the time when using R, you will not perform just the simple calculations you have seen in the previous section. For more complex cases, however, it is necessary to store the values, temporarily or permanently. In this section we are going to learn how to store the results of our calculations for later use.

In order to store a value, we first need to define a name which we will use to retrieve it. Let us call our stored value x. If you type in x into the console now, you will receive an error. That is because this object does not exist yet. By typing x <- 5, you will create the variable x and assign it the value 5. Now, typing x will return the value that you assigned. The assignment, expressed by the <- sign is always performed only after all the operations to the right of it have been finished.

Thus, you can also type in y <- 5+9. This will store the result of 5+9 under the name y. Remember, no matter how complex the calculation to the right of the assignment sign, only the final result will be stored.

Importantly, we can use the names we assigned not only to retrieve and view the values, but we can use them as variables in our calculations, too. Then, each time that calculation is performed, the name is replaced with the current value of the variable. So, every time you type in x <- x+5 the value of x will be increased by 5, assuming that x exists.

qtitle


Bisher wurde noch kein Kommentar abgegeben.