Practice: Calculator

To start getting familiar with R, go ahead and try the examples on your machine. Do you always get the output you expect? Create some variables (see the section on Variable Assignment) and check that they appear in the RStudio Environment (panel in the top right). This exercise does not count toward your grade. It is just for practice!

R
1 + 100

R
> 1 +

R
3 + 5 * 2

R
(3 + 5) * 2

R
(3 + (5 * (2 ^ 2))) # hard to read
  3 + 5 * 2 ^ 2       # clear, if you remember the rules
  3 + 5 * (2 ^ 2)     # if you forget some rules, this might help

R
2/10000

R
5e3  # Note the lack of minus here

R
getwd() #returns an absolute filepath

R
sin(1)  # trigonometry functions

R
log(1)  # natural logarithm

R
log10(10) # base-10 logarithm

R
exp(0.5) # e^(1/2)

R
1 == 1  # equality (note two equals signs, read as "is equal to")

R
1 != 2  # inequality (read as "is not equal to")

R
1 < 2  # less than

R
1 <= 1  # less than or equal to

R
1 > 0  # greater than

R
1 >= -9 # greater than or equal to

R
x <- 1/40

R
x

R
log(x)

R
x <- 100

R
x <- x + 1 #notice how RStudio updates its description of x on the top right tab
 y <- x * 2

R
x = 1/40

R
min_height
 max.height
 _age
 .mass
 MaxLength
 min-length
 2widths
 celsius2kelvin

R
min_height
 max.height
 MaxLength
 celsius2kelvin

R
.mass

R
_age
min-length
2widths

R
1:5

R
2^(1:5)

R
x <- 1:5
2^x

R
ls()

R
ls

R
x

R
rm(x)

R
rm(list = ls())

R
rm(list <- ls())


Source: The Carpentries, https://swcarpentry.github.io/r-novice-gapminder/01-rstudio-intro/index.html
Creative Commons License This work is licensed under a Creative Commons Attribution 4.0 License.

Last modified: Thursday, December 15, 2022, 4:45 PM