Episode

SNR - Episode 3 - Intro to R Data Structures

This episode takes a deep look at the out of the box data structures and data types available in R. After watching this video you should have an understanding of the basic types and structures in R and have a basic foundation of how they work and interact.

[00:00] - Introduction
[00:30] - R Basic Types
[14:00] - Vectors
[21:22] - Lists
[25:35] - Matrices
[31:24] - Data Frames
[38:50] - Not your typical Array
[48:12] - Conclusion

Code for the demo is below:

#Basic Data Types Demo
#install.packages("magrittr")
library("magrittr")
####---Numeric---####
x <- 10
y <- 15.3
class(x) #Defines what type it is
class(y) #defines what type it is
is.integer(x)
x + y
x - y
x * y
x / y

####---Integer---####
x <- as.integer(x)
x
class(x)
z <- x + y
class(z)
z
class(y)
class(x)
y <- as.integer(y)
y
class(y)
x + y

####---Complex Numbers---####
x <- 1i
class(x)
y <- -1
class(y)
z <- x + y
class(z)
sqrt(y)
y <- y + 0i
sqrt(y)
class(y)

####---Logical Values---####
x <- 0
y <- 2
x > y
x < y
x <- as.logical(x)
y <- as.logical(y)
x & x
x | y
!x
x | !y

####---Character Values---####
x <- "Data4Bots"
y <- as.character(y)
paste(x, y)
paste(x,y, sep = '')
paste(x, y, sep = ' FOLLOW_ME_NOW_')

####---Vectors---####
x <- c(1,2,3,4,5)
class(x)
x
x + 2
x * 2
y <- c("David", "Lydia", "Alice")
y
y <- paste(y, "Crook")
y %>% paste("Crook")
length(y)
y[1]
z <- c(x, y)
summary(z) #notice z is all characters now
z <- as.numeric(z)
summary(z)
z

####---Lists---####
x <- list(1, 1i, "cc")
x
class(x)
x <- c(1,2,3,4)
y <- c("David", "Lydia", "Alice")
z <- list(x,y)
z
class(z)
length(z)
z[1]
z[[1]]
z[[1]][1]
z[[2]][2]
z <- list(z,x) #a list can even contain a list and a vector
z

####---Matrices---####
x <- matrix(
c(1, 2, 3, 4, 5, 6, 7, 8),
nrow <- 2,
ncol <- 4,
byrow <- TRUE
)
x #notice output notation
x[,1]
x[1,]
x * 2
x[1,] <- x[1,] * 2
x
as.character(x[1,])
x[1,] <- as.character(x[1,])
x

####---Data Frames---####
F1.Name <- c("F1 R1", "F1 R2", "F1 R3") #All Rows for Feature 1
F2.Name <- c("F2 R1", "F2 R2", "F2 R3") #All Rows for Feature 2
F3.Name <- c("F3 R1", "F3 R2", "F3 R3") #All Rows for Feature 3
df <- data.frame(F1.Name,F2.Name,F3.Name) #Combine for Data Frame
df$F1.Name
paste(df$F1.Name, "PASTED")
df$F1.Name <- paste(df$F1.Name, "PASTED")
df
df[1,]
#Mixed Types
F1.Name <- c(1, 1, 3) #All Rows for Feature 1
F2.Name <- c(1, 1, 6) #All Rows for Feature 2
F3.Name <- c("F3 R1", "F3 R2", "F3 R3") #All Rows for Feature 3
df <- data.frame(F1.Name,F2.Name,F3.Name) #Combine for Data Frame
df[,1]
df$F1.Name * df$F2.Name
paste(df$F1.Name * df$F2.Name, df$F3.Name)
summary(df)

####---Arrays---####
x <- c(1,2,3,4,5,6)
y <- c(7,8,9,10,11,12)
z <- array(c(x,y), dim = c(3,2,2))
z
col.names <- c("c1", "c2")
row.names <- c("1", "2", "3")
matrix.names <- c("m1", "m2")
names <- list(row.names, col.names, matrix.names)
z <- array(c(x,y), dim = c(3,2,2), dimnames = names)
z
z[3,1,2] #3rd Row, 1st Column, 2nd Matrix
z[3,,] #3rd Row, all columns, all matrices
z[,1,] #1st Column, all rows, all matrices
z[,,1] #first matrix
z * 2
summary(z)
plot(z)

SQL
R