Introduction to R (part 2)

Photo by Mika Baumeister on Unsplash

Basic data types in R

Constants in R

# Change my_numeric to be 42
my_numeric <- 42
# Check class of my_numeric
class(my_numeric)
[1] 'numeric'
typeof(5)
[1] "double"
typeof(5L)
[1] "integer"
typeof(5i)
[1] "complex"
0xff
[1] 255
0XF + 1
[1] 16
> 'example'
[1] "example"
> typeof("5")
[1] "character"
# Change my_character to be “universe”
my_character <- “universe”
# Check class of my_character
class(my_character)
'character'
# Change my_logical to be FALSE
my_logical <- FALSE
> LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
> letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
> pi
[1] 3.141593
> month.name
[1] "January" "February" "March" "April" "May" "June"
[7] "July" "August" "September" "October" "November" "December"
> month.abb
[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

Logical

my_logical <- FALSE# Check class of my_logical
class(my_logical)
[1] ‘logical

R Arithmetic Operators

x <- 5
y <- 16
> x+y
[1] 21
x-y
[1] -11
x*y
[1] 80
y/x
[1] 3.2
y%/%x
[1] 3
y%%x
[1] 1
y^x
[1] 1048576

R Relational Operators

x <- 5
y <- 16
x<y
[1] TRUE
x>y
[1] FALSE
x<=5
[1] TRUE
y>=20
[1] FALSE
y == 16
[1] TRUE
x != 5
[1] FALSE

Mathematical functions

log(1:3) [1] 0.0000000 0.6931472 1.0986123 
1000^2 [1] 1e+06 
round(123.456, digits = 2) [1] 123.46 
round(-123.456, digits = -2) [1] -100 
signif(-123.456, digits = 4) [1] -123.5 
cos(120) [1] 0.814181