Basic calculations in R

In simple terms, R is a calculator: You enter an equation and it returns the answer.

Let’s try this with a few examples.

Type in “1+1” in your R console. Note that the ## below is the answer returned by R.

1+1

## [1] 2

16/4

## [1] 4

6^8.5

## [1] 4114202

You can add comments to your code in the script using #. This way, R will understand that this part of your script is a comment and as such will not execute this part. Using comments in your script is useful, because it explains what your code is doing to the others who are reading your code or to yourself after returning to it after some time. You should comment your code as much as possible. It will make your life easier!


# This is my first calculation in R:

1+1

## [1] 2

In addition to performing calculations, you can also store information you have created. In the upper right hand corner of RStudio you see all variables you have created in that session.

x <- 1

y <- 2+2

To print the information you have stored you can either type in the name of the variable or do print().

y

## [1] 4
print(y)
## [1] 4

You can perform operations on the variables you have created.

x+y

## [1] 5 

We call these variables you create OBJECTS. You have almost complete liberty to name your object.

silly_name <- 5+3

silly_name

## [1] 8

access_time Last update May 8, 2020.

chat networking coding local-network layer menu folders diagram panel route line-chart compass search flow data-sharing search-1 message target translator candidates studying chat networking coding local-network layer menu folders diagram panel route line-chart compass search flow data-sharing search-1 message target translator candidates studying