################################################################################
#### Module 3, Lesson about Vectors, Basic Functions, Data Frames, and Help ####
################################################################################
# Lesson 4, part 3: Named vectors, more indexing, and modifying vectors


# First reload our diam_cm_clean vector from the last part: 
diam_cm_clean <- c(12,18,32,8,26,21,18)

###################################
##### NAMED VECTORS
# You can also name the elements of a vector.

species <- c("spp1","spp2","spp3","spp4","spp5","spp6","spp7")
names(diam_cm_clean) <- species
diam_cm_clean

diam_cm_clean[c('spp3','spp5')]

###################################
##### LOGICAL INDEXING
# Probably the most common way you will do indexing in R

is_large <- diam_cm_clean > 20

diam_cm_clean[is_large]

diam_cm_clean[diam_cm_clean > 20]

medium_diams <- diam_cm_clean[(diam_cm_clean < 20) & (diam_cm_clean > 10)]



###################################
##### MODIFYING VECTORS WITH INDEXING

my_vector <- c(2,4,2,23,65,13,5,67,25,42,32,12,15,17)

my_vector[my_vector > 20] <- 40

my_vector_mod <- my_vector
my_vector_mod[my_vector_mod > 20] <- 40

my_vector_mod2 <- my_vector
my_vector_mod2[8] <- NA


###################################
##### SUMMARY
# - Named vectors
# - Logical indexing of vectors
# - Changing vector values using indexing
