Using R as your calculator
sin(2*pi/3) # <--- this symbol is for comments.
## [1] 0.8660254
5^2 # Same as 5*5.
## [1] 25
sqrt(4) # Square root of 4.
## [1] 2
log(1) # Natural logarithm of 1.
## [1] 0
c(1,2,3,4,5) # Collection of the first 5 integers.
## [1] 1 2 3 4 5
c(1,2,3,4,5)*2 # First five even numbers.
## [1] 2 4 6 8 10
R datatypes: can use typeof() to find the data type
# Numeric
x <- 7.3
print(typeof(x)) # Will print "double"
## [1] "double"
# Integer
x <- 5L
print(typeof(x)) # Will print "integer"
## [1] "integer"
# Complex
x <- 3 + 2i
print(typeof(x)) # Will print "complex"
## [1] "complex"
# Logical
x <- TRUE
print(typeof(x)) # Will print "logical"
## [1] "logical"
# Character
x <- "Hello, World!"
print(typeof(x)) # Will print "character"
## [1] "character"
R also include objects including vectors, data frames etc.
# Vector
x <- c(7.3, 5.4, 9.7) #must be same data types
print(x)
## [1] 7.3 5.4 9.7
# List
x <- list("Hello", TRUE, 5L, 7.3) #can be different data types
print(x)
## [[1]]
## [1] "Hello"
##
## [[2]]
## [1] TRUE
##
## [[3]]
## [1] 5
##
## [[4]]
## [1] 7.3
# Matrix
x <- matrix(1:9, nrow=3, ncol=3)
print(x)
## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
# Data frame
x <- data.frame(name=c("Alice", "Bob"), age=c(25, 31))
print(x)
## name age
## 1 Alice 25
## 2 Bob 31
Now we are ready to generate random observations in R (P.74)
# Random floating number between 0 and 1
random_float <- runif(1)
print(random_float)
## [1] 0.5209113
# Four random observations from a Uniform [2,7]
runif(n = 4, min=2, max=7)
## [1] 2.638357 6.127518 6.612049 6.239219