Perform computational tasks with R Important operators and functions in R
The focus of R is the analysis of data. For this purpose, calculations are of course also carried out with the help of operators and functions. In this post we give a first introduction to this area in R.
Companies on the topic
Calculations can be mapped with R directly via operators or functions.
In R, arithmetic and logical operators are integrated, which can facilitate the entry. These can be used quickly and easily in the R Studio. In general, first calculations can be carried out very quickly with R.
If you already want to comment out lines in the R-Studio or the command line, the character # is available. Operators and functions can be used to work with the language directly after the installation of R. In the console of R-Studio it is possible to enter commands directly. The R-Studio then executes this and displays the result in the window.
R is case sensitive, so it distinguishes between case and case sensitive. This is important when using functions, because LOG(1) displays an error message because the function is called “log”. For calculations, it may also be necessary to empty the console in R-Studio. The key combination CTRL + L is normally used for this purpose.
Operators and functions to get started
Operators can be used in R to perform calculations even without code – for example, simple calculations such as “1+2”. Basically, operators and functions in R are largely identical, because operators can also be used as a function. The most famous arithmetic operators in R are:
+ for Addition
– for subtraction
* for multiplication
/ for Division
^ for potency
sqrt() for root
To use an operator as a function, you need to set the operator in high commas. Example:
- 1 + 2 (Use as operator)
- ‘+'(2, 3) (Use as function)
To perform calculations, simply enter them in the console. You do not have to write spaces between the numbers and the operators, but they do not hurt and improve readability. R uses the decimal point, not the comma.
In addition to these operators, there are also more complex ones, such as integer division (x %% Y, for example 9 %/% 2) or matrix multiplication (%*%). In addition to the operators, functions also play an important role. The most famous are:
< Smaller and smaller
& lt;= Less than or equal to
& gt;= Larger than or equal to
!x Not x
x / y x or y
x & amp; y x and y
exp (x) Expo potential function
cos(x) Trigonometric functions such as cosine and sine
The functions shown here are just examples for getting started. There are numerous other functions for R, also for the statistics area, such as var(x) for variance, or the standard deviation “sd(x)”. Functions can also be nested, just like operators. An example is: log(exp(2)). In general, R works from the inside out when performing nested functions.
Perform computational tasks with R
Computational tasks can be entered directly in R-Studio in the code window or console, for example “1+2” or “5/2”. Extended calculations can also be used if you put them in brackets, just like in other programs. R knows the mathematical rules, such as point before line and parentheses of course, for example: (1 + 4 – 2 + 10) / (3 + 8 + 3). The operators and functions can be used to perform calculations, even in complex form.
Of course, variables also play an important role in the context of operators and functions. You can store results or numbers directly in variables and also perform these calculations. Variables are assigned with the two characters “<” and ” -“.
In parallel, variables can also be assigned with”=”. However, the equal sign is also used for functions. For this reason, it is better to outstaffing work unambiguously with “<-” in program code. But if you calculate in the console, you can work with the equal sign.
In R-Studio you can also use the key combination “Alt+ -“. Variables must begin with a letter and must not contain spaces. For example, you can use” x <- 10 ” to assign the value 10 to variable x. Use ” x ” to display the variable in the console.
If you work with several variables, they can be included in the calculation. In this example, “y <- x +1” can be used to assign the value “x+1” to the variable “y”, i.e. 10. The R console displays the result with “y”. To integrate both variables into an invoice, they are simply linked to an operator or a function, for example with “x*y”.
In this example, it is now possible to define a third variable and calculate it from other variables, for example: “z <- x/y”. Variables can take more than one value. For example, the function “c()” (abbreviation for: combine) can be used. As an example, more than one value can be assigned to the variable “house number” with “House number <- c(1,3,5).
By entering “house number” these values are also displayed. Here, too, care must be taken that R is case sensitive. If variables are also to be assigned texts, for example “Hauptstrasse” and “Dorfstrasse”, then the texts are written in quotation marks, for example:
Strasse <- c(„Hauptstrasse“, „Dorfstrasse“)
Variables can also be extended. For this purpose, the name of the variable itself is added to the list of data of a variable and in addition the new values, for example:
Strasse <- c(Strasse, „Oststrasse“)
If numbers are used in the variables, they can also be queried in different ways. Examples are:
mean (house number) – Mean
House number[1:3] – First and third value of the variable.