## R commands modified from Matrices_and_Data_Frames

my_vector <- c(4, 2, 5)    # create the vector (4, 2, 5) called my_vector
my_vector                  # Display my_vector
my_vector <- 1:20          # create a vector called my_vector containing the numbers 1 through 20
                           # WARNING:  we wrote over our old my_vector
my_vector                  # Display my_vector
dim(my_vector)             # Observe dim does not work for vector
length(my_vector)          # But length does

dim(my_vector) <- c(4, 5)  # Assign dimension to my_vector
                           # my_vector is now a matrix with 4 rows and 5 columns
class(my_vector)           # we can see that my_vector is now a matrix
dim(my_vector)             # dim returns its dimensions
my_vector                  # display my_vector
attributes(my_vector)      # See attributes (i.e., dimension) of this matrix 

my_matrix <- my_vector     # copy my_vector into my_matrix

my_matrix2 <- matrix(c(1, 2, 3), nrow=4, ncol=5)   # create a 4x5 matrix
                           # Note matrix is filled with repeating 1, 2, 3
                           # Note warning since 3 does not divide 20   
my_matrix2                 # display

my_matrix2 <- matrix(c(1, 2, 3, 4, 5), nrow=4, ncol=5)   #create a 4x5 matrix
                           # Note matrix is filled with repeating 1, 2, 3, 4, 5
                           # Note there is NO warning since 5 divides 20   
my_matrix2                 # display

my_matrix2 <- matrix(1:20, nrow=4, ncol=5)   #create a 4x5 matrix
my_matrix2                 # display


identical(my_matrix, my_matrix2)   # check if the two matrices are the same

patients <- c("Bill", "Gina", "Kelly", "Sean")

cbind(patients, my_matrix)  # combine as columns: patients vector and 
                            # my_matrix into a single matrix and display
                            # Note that since we did not assign it a name,
                            # we are not saving it to work with it later

my_data <- data.frame(patients, my_matrix)   # We instead combine patients and
                                             # my_matrix into a data frame 
                                             # which we call my_data
my_data
class(my_data)
cnames <- c("patient", "age", "weight", "bp", "rating", "test")
colnames(my_data) <- cnames       # assign names to each of the columns
my_data
my_data$age               # the age column of my_data
my_data[,2]               # the 2nd column of my_data
my_data[1,]               # the 1rst row of my_data
my_data[1,2]              # the element in 1rst row, 2nd columns of my_data                   
range(my_data$age)        # calculate the range: lowest value, highest value
range(my_data[,2])
mean(my_data$age)         # calculate the mean 
sum(my_data$age)/4        # calculate the sum and divide sum by 4
median(my_data$age)       # calculate the median
sd(my_data$age)           # calculate the standard deviation
var(my_data$age)          # calculate the variance
sqrt(var(my_data$age))    # square root of variance = standard deviation
mode(my_data$age)         # Get the type or storage mode of an object.
?mode
sort(my_data$age)         # Sort the data in increasing order         
?sort
sort(my_data$age, decr = TRUE)         # Sort the data in decreasing order 
summary(my_data$age)
summary(my_data)

hist(my_data$age, xlab="age", main="Title")
hist(my_data$age, main="", breaks = 4, col = "lightblue", border = "black")
hist(my_data$age, main="", breaks = 5, col = "lightblue", border = "pink")
hist(my_data$age, xlab="age", main="", breaks = c(0, 1, 2, 3, 4), col = "lightblue")
?hist

plot(my_data)
plot(cbind(my_data$age, my_data$bp))   # First used cbind to create matrix with 
                                       # 2 columns: my_data$age, my_data$bp
                                       # Then used plot to plot these points  
cbind(my_data$age, my_data$bp)

2*my_data           # for fun I multiplied my_data by 2.  Observe the output.

rm(my_data)               # remove my_data
my_data                   # note my_data no longer exists. 

rm(list=ls(all=TRUE))     # removes ALL your Data!!!!!!!!!!!
                          # NOTE:  ALL values are ERASED!!!!  
