In this class, you will learn how to:

#Importing the deck dataset and displaying the 1st 7 rows.
deck <- read.csv("deck.csv",stringsAsFactors=FALSE)
head(deck)

#Positive Numbers R treats positive integers just like ij notation in linear algebra: deck[i,j] will return the value of deck that is in the ith row and the jth column,

#display the 1st column and row element from the 'deck' dataset.
  #Attach a new vector showing the 1st row and 1,2,3 columns deom the 'deck' dataset to 'new.'
deck[1, 1]
## [1] "king"
deck[1, c(1, 2, 3)]
new <- deck[1, c(1, 2, 3)]
new

Repetition

#Show the 1st row and 1,2,3 columns, then repat the same thing below it.
deck[c(1, 1), c(1, 2, 3)]

Returns a Data Frame

#return rows 1 & 2, and columns 1 & 2
deck[1:2, 1:2]

Returns a vector

#return the elements that are in the 1st column of the 1st and 2nd row.
deck[1:2, 1]
## [1] "king"  "queen"

Returns a data frame

#When drop is FALSE , the dimensions of the object are kept.
#Return the 1 column elements from the 1st and 2nd row in its proper dimensions
deck[1:2, 1, drop = FALSE]

#Negative Numbers

#return all 1,2,3 columns for rows other than 2-52; which ends up being the 1st one
deck[-(2:52), 1:3]

Illegal instruction.

#There cannot be a - # in the quotation.
#deck[c(-1, 1), 1]

#Zero The following instruction creates and empty object.

#create and emmpty object with no rows or columns
deck[0, 0]

#Blank Spaces

#Returns all columns for the 1st row.
deck[1, ]

#Logical Values

# In numeric and complex vectors, zeros are FALSE and non-zero values are TRUE. 
  #Character strings c("T", "TRUE", "True", "true") are regarded as true
  #c("F", "FALSE", "False", "false") as false, and all others as NA.
#returns the 1 row with colums that 1st have an a non-zero value while the other do.
deck[1, c(TRUE, TRUE, FALSE)]

Lets check the attributes function again

#shows the characteristics of the elements. 
rows <- c(TRUE, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
F, F, F, F, F, F, F, F, F, F, F, F, F, F)
str(rows)
##  logi [1:52] TRUE FALSE FALSE FALSE FALSE FALSE ...

Extract elements based on the name

#Return the 1st row, and following columns for face, suit, & value
deck[1, c("face", "suit", "value")]

#Names You can use a blank space to tell R to extract every value in a dimension.

# the entire value column for the 1st row
deck[ , "value"]
##  [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2
## [26]  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3
## [51]  2  1

Complete the following code to make a function that returns the first row of a data frame:

#deal <- function(cards) {
# ? }
#Return 1st row of Data
code1 <- deck[1,]
code1

Shuffle deck

#Return all the columns for rows 1-52, then display the 1st 7 rows.
deck2 <- deck[1:52, ]
head(deck2)
#return rows 2 then 1 then 3-52 for all the columns
deck3 <- deck[c(2, 1, 3:52), ]
deck3

#How Modify Order

#Sample() function is used to generate the random elements from the given data with or without replacement.
#First create a vector of 52 numbers in random order and store it in an object named random.
  #size= means the size of a returned list; a non-negative integer giving the number of items to choose.
random <- sample(1:52, size = 52)
random
##  [1]  1 47 50 44 35  5  4 48 27 26 51 33 40 13 21 10 37 43  9 31 42 20 14 17 46
## [26] 15  7 25 30 49 28 23 41 22 12  8 52  6 18 32 19 39 38 36  2 24 29 11  3 16
## [51] 45 34
#From the deck dataset get the rows from 'random'then all the columns following it.
  #then display the 1st 7 rows.
deck4 <- deck[random, ]
head(deck4)

#Dollar Signs and Double Brackets

Two types of object in R obey an optional second system of notation. You can extract values from data frames and lists with the $ syntax. You will encounter the $ syntax again and again as an R programmer, so let’s examine how it works. To select a column from a data frame, write the data frame’s name and the column name separated by a $. Notice that no quotes should go around the column name:

#return the 'value' columne from the deck dataframe
deck$value
##  [1] 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3  2
## [26]  1 13 12 11 10  9  8  7  6  5  4  3  2  1 13 12 11 10  9  8  7  6  5  4  3
## [51]  2  1
#get the mean from the 'valve' column on the deck dataset
mean(deck$value)
## [1] 7
#get the median from the 'valve' column on the deck dataset
median(deck$value)
## [1] 7

To see this, make a list:

#create a list with 3 characters that each contain their own elements. 
lst <- list(numbers = c(1, 2), logical = TRUE, strings = c("a", "b", "c"))
lst
## $numbers
## [1] 1 2
## 
## $logical
## [1] TRUE
## 
## $strings
## [1] "a" "b" "c"

And then subset it:

#get the 1 character from the 1st list. 
lst[1]
## $numbers
## [1] 1 2

The result is a smaller list with one element. That element is the vector c(1, 2). This can be annoying because many R functions do not work with lists. For example, sum(lst[1]) will return an error. It would be horrible if once you stored a vector in a list, you could only ever get it back as a list:

#Error!!
#sum(lst[1])

When you use the $ notation, R will return the selected values as they are, with no list structure around them:

#Return the elements from the number column of the 1st list.
lst$numbers
## [1] 1 2
## 1 2

You can then immediately feed the results to a function:

#sum the created data. 
sum(lst$numbers)
## [1] 3
## 3

If the elements in your list do not have names (or you do not wish to use the names), you can use two brackets, instead of one, to subset the list. This notation will do the same thing as the $ notation:

#[[]] will subset a list and not completly change it.
lst[[1]]
## [1] 1 2
## 1 2

In other words, if you subset a list with single-bracket notation, R will return a smaller list. If you subset a list with double-bracket notation, R will return just the values that 74 | Chapter 4: R Notation were inside an element of the list. You can combine this feature with any of R’s indexing methods:

#1- Displays the elements of a list with the column name, while the 2- display the elements themselves.  
lst["numbers"]
## $numbers
## [1] 1 2
lst[["numbers"]]
## [1] 1 2

#Changing Values in Place

#created a concatenated '0' for vec.
vec <- c(0, 0, 0, 0, 0, 0)
vec
## [1] 0 0 0 0 0 0
#Return the 1 vector with '1000' starting.
vec[1] <- 1000
vec
## [1] 1000    0    0    0    0    0

Changing more than one value at the same time.

#for the 1st, 3rd, 5th element of vec to be switched for '7.9.8'
vec[c(1, 3, 5)] <- c(7, 9, 8)
vec
## [1] 7 0 9 0 8 0
#for the columns of 4-6, nd add 1 to it.
vec[4:6] <- vec[4:6] + 1
vec
## [1] 7 0 9 1 9 1

Force the vector to grow.

#for the 7th element put '0'
vec[7] <- 0
vec
## [1] 7 0 9 1 9 1 0
#import the deck dataset into deck2, and get the 'new' column for 1-52, and displaying the 7 1st datasets
deck2 <- read.csv("deck.csv",stringsAsFactors=FALSE)
deck2$new <- 1:52
head(deck2)

Remove a column from a dataframe.

#delete the 'new' column from the deck2 dataset.
deck2$new <- NULL
head(deck2)
#Display the 13,26,39,52 rows with all the columns
deck2[c(13, 26, 39, 52), ]
#Display the 13,26,39,52 rows with the 3rd columns
deck2[c(13, 26, 39, 52), 3]
## [1] 1 1 1 1
#Only show elements from the value column for 13,26,39,52.
deck2$value[c(13, 26, 39, 52)]
## [1] 1 1 1 1
#For the 13,26,39,52 rows of the 'valve' columns replace the values with 14.
deck2$value[c(13, 26, 39, 52)] <- c(14, 14, 14, 14)

```