###################################################################
#### Module 3, Lesson about Vectors, Basic Functions, and Help ####
###################################################################
# Lesson 6, part 1: Intro to dataframes (indexing and filtering)


###################################
##### WHAT ARE DATAFRAMES
# You can think of dataframes as spreadsheets, or a series of
# vectors that are bound together in the same order. Different columns
# in a data frame can be of different variable types.

species <- c("spp1", "spp2","spp3","spp4", "spp5")
height <- c(57,45,62,49,58)

height_data <- data.frame(species, height)

names(height_data)
row.names(height_data)

nrow(height_data)
ncol(height_data)

length(height_data)

str(height_data)

###################################
##### INDEXING AND FILTERING DATAFRAMES
# As with vectors we can also index data frames but this time
# we have more than one dimension--we have both rows and columns:

height_data[4,2]

height_data[,"height"]

height_data[,1]

height_data[2:4,]

height_data[,"species"]
height_data$species

data(ChickWeight)

head(ChickWeight)

levels(ChickWeight$Diet) <- c("corn", "grain", "rice", "chocolate")

young <- ChickWeight$Time < 5

young_chicks <- ChickWeight[young,]

table(young_chicks$Time)

ChickWeight[ChickWeight$Time < 5, ]

ChickWeight[ChickWeight[,"Time"] < 5, ]

ChickWeight[ChickWeight[,2] < 5, ]


levels(ChickWeight$Diet) <- c("corn_a_lot", "corn_little", "rice_trt", "chocolate_trt")

corn_treatments <- grep(pattern="corn", x=ChickWeight$Diet)
corn_treatments_logical <- grepl(pattern="corn", x=ChickWeight$Diet)

corn_data <- ChickWeight[corn_treatments,]

corn_data <- ChickWeight[grep(pattern="corn", x=ChickWeight$Diet),]


###################################
##### SUMMARY
# - what are data frames
# - indexing and filtering

