Skip to content

Latest commit

 

History

History
204 lines (134 loc) · 1.65 KB

first.md

File metadata and controls

204 lines (134 loc) · 1.65 KB

First steps in R

R is a powerful calculator with standard arithmetic operators:

# Addition
1 + 6
## [1] 7
# Multiplication
2 * 6
## [1] 12
# multiplication and division first, then addition and subtraction
2 * 6 + 1
## [1] 13
# use of brackets
2 * (6 + 1)
## [1] 14
# square root
sqrt(16)
## [1] 4
# powers
16^0.5
## [1] 4
2^2
## [1] 4
# natural log
log(10)
## [1] 2.302585
# decadic log
log10(10)
## [1] 1
# log to base of 4
log(10, 4)
## [1] 1.660964
# exponential
exp(2)
## [1] 7.389056
# pi is a built-in constant
pi
## [1] 3.141593
# but not e
e^2
## Error in eval(expr, envir, enclos): object 'e' not found

If we want to refer to a result later on, we can give them a name.

a <- 1 + 6

Here I assign (<-) the result of 1 + 6 to an object named a. If you just type the name into the console R shows whats inside this object:

a
## [1] 7

You can also do calculations with this object:

2 * a
## [1] 14

Note, that R will overwrite objects:

a
## [1] 7
a <- 2*a
a
## [1] 14

If R doesn't know about an object you get the following error:

b
## Error in eval(expr, envir, enclos): object 'b' not found

which says: "I looked for the object 'b', but could not find it"".

You can see objects that are available in the Environment pane of RStudio. Alternatively you can use the ls() function.