This is the practice assignment from Class 2. For this assignment, I perform some basic math functions and data type manipulations.
First, we execute some basic (and not-so-basic) math functions.
#A little addition
3 + 4 + 13
## [1] 20
#Subtracting one from another
10 - 5
## [1] 5
#Multiplication
5 * 7
## [1] 35
#Division
25 / 5
## [1] 5
#Powers: Three to the power of four
3^4
## [1] 81
#Calculating the cube root to 64, which is the same as raising 64 to the 1/3 power
64^(1/3)
## [1] 4
#Calculationg LOG Base 10 of 20
log(20,10)
## [1] 1.30103
Next, we will create a vector, which is a series or array of numbers (coudl be characters) represented by a single variable. Then, we create a matrix, which resembles a table of values represented by a single variable.
#Assigning a vector to 'c'
c <- c(1,2,3,5,7,11)
#Printing the third value of the array, which is, consequently, also a three!
c[3]
## [1] 3
#Printing the number of values included in the vector 'c'.
length (c)
## [1] 6
#Next, we create a matrix, which is like a table, and assign it to 'd'
d <- matrix(c(1,3,5,7,2,4,6,8), nrow=2)
#Assigning the variable. Note: R does care about the case!
F <- d + 5
#Printing the new matrix:
F
## [,1] [,2] [,3] [,4]
## [1,] 6 10 7 11
## [2,] 8 12 9 13
#Printing the matrix dimension:
dim(F)
## [1] 2 4
Next, we work with some data type commands.
#First, we assign a double and a character to two separate variables.
k <- 5.25
m <- "frank"
#Next, we identify the data types R assigned to these variables.
typeof(k)
## [1] "double"
typeof(m)
## [1] "character"
#We change the type of data type from double to integer. Note: we no longer have decimals, as expected.
p <- as.integer(k)
p
## [1] 5
#We create G and transform the double data type into a character.
G <- as.character(k)
G
## [1] "5.25"
Finally, we attempt to add one type of data (double) to an incompatible type of data (character):
G + 5.1
Yikes! This creates an error. The .rmd can’t compile it. You can’t add a character to a number. It doesn’t “add up”!