################################################################################
#### Module 3, Lesson about Vectors, Basic Functions, Data Frames, and Help ####
################################################################################
### EXCERCISES (Lessons 4, 5, 6)

# 1) Define a variable named "my_var" with a vector of sequential numbers from 20 to 35:
my_var <- 20:35

# 2) Use another method to do the same thing
my_var <- seq(20, 35, 1)

# 3) Multiply each element of this vector by 5 and save this as a new vector named my_var_x5
my_var_x5 <- my_var * 5

# 4) Add my_var to my_var_x5 and save this as my_var_final
my_var_final <- my_var_x5 + my_var

# 5) Finally, index all values of my_var_final that are greater than 200
my_var_final[my_var_final > 200]

# 6) Create a vector of the sequence "1, 2, 3" that repeats itself 5 times.
rep(x = c(1,2,3), times = 5)

### Now load the chickenweight data using: data(ChickWeight)
# 7) View the top few observations (rows) in these data:
head(ChickWeight)

# 8) How many rows and columns does this dataframe have?
nrow(ChickWeight)
ncol(ChickWeight)

# 9) Extract and define the "Time" column of these data as its own vector two different ways:
times <- ChickWeight$Time
times <- ChickWeight[,"Time"]

# 10) Index the weight of the chicken at the 8th row of data
ChickWeight[8,"weight"]
ChickWeight[8,1]

# 11) Index the weight of the first five and the last five observations and save this as a 
# new vector named my_weights:
indices <- c(1:5, 245:250)
my_weights <- ChickWeight[indices,"weight"]

# 12) How would we get the last 5 weight values if we didn't know the length of the data
# frame?
indices <- c((nrow(ChickWeight)-5):(nrow(ChickWeight)))
ChickWeight[indices,"weight"]

# 13) It turns out that weight observations 10, 24, and 132 are faulty data. Replace these values with NAs:
ChickWeight[c(10,24,132),"weight"] <- NA
ChickWeight

# 14) Calculate the average weight observations in this dataframe and save this as a new variable:
mean_weight <- mean(ChickWeight$weight, na.rm=T)

# 15) What is the largest weight recorded in this dataset?
max(ChickWeight$weight, na.rm=T)

# 16) Create a vector of 20 random integers from 1 through the number of rows in ChickWeight. Then index the 
# ChickWeight data to only those rows and define it as a new dataframe called chick_wt_sub.
# Use the set.seed() function before in order to get the same results I did:
set.seed(123)
rand_samp <- sample(1:nrow(ChickWeight), 20)

chick_wt_sub <- ChickWeight[rand_samp,]

# 17) Let's say we need to transform the chicken weights from grams
# into pounds and all observations with a weight greater than 0.25 lb should be converted to NA.
# Create a custom function that does this transformation to the weight data in our ChickWeight dataframe.
# hint, 1 gram equals about 0.0022 lbs.

# define the function:
weight_trans <- function(weights_g){
  # transform weights into lb:
  weights_lb <- weights_g * 0.0022
  # find which weights are greater than 0.25 lb:
  weights_large <- weights_lb < 0.25
  # replace large weights with NA values:
  weights_lb[weights_large] <- NA
  # return the transformed weights vector:
  return(weights_lb)
}
chick_weight_trans <- ChickWeight
chick_weight_trans$weight <- weight_trans(ChickWeight$weight)

# 18) Add a new column in the this dataframe called life_stage, where "early" indicates times less 
# than or equal to 10 and "late" indicates times greater than 10:
chick_weight_trans$life_stage[chick_weight_trans$Time <= 10] <- "early"
chick_weight_trans$life_stage[chick_weight_trans$Time > 10] <- "late"

# 19) Add a new column that converts time in days (current Time column) to time in hours, and rename
# the columns so that they are all clear, informative, and consistent:
chick_weight_trans$time_hours <- chick_weight_trans$Time * 24
names(chick_weight_trans)[1:4] <- c("weight_g","time_days","chick_id","diet")
head(chick_weight_trans)

# 20) Finally, order this resulting dataframe by time_hours (earliest times first):
chick_weight_trans_ordered <- chick_weight_trans[order(chick_weight_trans$time_hours),]
head(chick_weight_trans)

### That's it!


