2. Using R as a calculator


It is fairly easy to perform simple arithmetic operations in R. You simply need to type in the operations as command directly in the editor (script), right after the > sign, and press ENTER to validate.

Use +, -, * and / for additions, subtractions, multiplications and divisions, respectively.

Try to copy the following code in your script window to try it on your own (do not copy the line numbers…):

[code language=”r”]
5+3
7-4
7*9
8/2
[/code]

operationsNote that the result lines (in blue) always starts with [1] in our example. This number between brackets is only there to indicate the position of the result in the displayed line of answers that your command might have created. Don’t worry, this will make much more sense later on.

 

 

 

 

 

 

There are a few functions that you may use to perform other simple, standard operations on numbers. Among these are sqrt(), exp(), log()

[code language=”r”]
sqrt(4)
log(4)
exp(4)
[/code]

functions

 

 

 

 

 

 

 

At anytime, you may also combine operators and functions to perform more complex operations. You may additionally use parentheses whenever needed.

[code language=”r”]
sqrt((9+7)*4)
[/code]

combination

  Fant du det du lette etter? Did you find this helpful?
[Average: 0]