options(stringsAsFactors = FALSE)
#################### setup
## define the vectors
poker_vector <- c(140, -50, 20, -120, 240)
roulette_vector <- c(-24, -50, 100, -350, 10)
## we are covering named vectors in the module 3 Friday
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
names(poker_vector) <- days_vector
names(roulette_vector) <- days_vector
## which days did we make money on poker
selection_vector <- c(poker_vector)>0
selection_vector
## why is this code different (your question)
selection_vector <- poker_vector[c(1:5)>0]
############### explanation 1
c(1:5) # is just a vector, and because you are using :, you actually dont need the c
# c(1:5) == 1:5
# identical(c(1:5), 1:5)
#the vector of 1:5 is always greater than zero
1:5 > 0
## in short, above, you arent using the positional indexes, but just the vector of 1:5
## compared to zero.
## This question is asking for the evaluation of the values against zero
# simply you are passing in 5 trues which returns the entire poker_vector
poker_vector[1:5 > 0] == poker_vector ## one way to check
identical(poker_vector[1:5>0], poker_vector) #check if the objects are identical
# but when we ask if the vector is > 0
poker_vector > 0
# we see that there are trues and falses
# we use the T/F to select only the positive values, the TRUEs
selection_vector2 <- poker_vector > 0
# and now just return the positive values
poker_vector[selection_vector2]
################# explanation 2
selection_vector3 <- poker_vector[c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")>0]
## inside the poker vector, you are doing a logical comparison of strings against zero
c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")>0
## simply, above, you are comparing a character vector, not the rows (based on name) to zero
## show this a different way
dow <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
dow > 0
is.character(dow)
is.vector(dow)
typeof(dow)
## the comparison of strings to zero is bizarre, but its based on the comparison of characters
## and basically alphasorted check
## however, because they are all TRUEs, the entire vector is returned
##### summary
## it doesnt hurt to use the c(1:5), but I showed above that it is identical to 1:5, so the c is overkill but not a bad thing as you are learning
## you need to compare vector values element wise
## if you wanted to compare random elements , you do the comparison outside
poker_vector[c(2,4,5)]
poker_vector[c(2,4,5)] > 0
## note above I used the c because i am not defining a vector by a sequence, but am building it with c