- To modify value within an R object:
- First, describe the value (or values) you wish to modify.
- Use the assignment operator
`<-`

to overwrite those values. - R will update the selected values
*i the original object*.

- To modify value within an R object:
- First, describe the value (or values) you wish to modify.
- Use the assignment operator
`<-`

to overwrite those values. - R will update the selected values
*i the original object*.

x <- c(0, 0, 0, 0, 0, 0) x

## [1] 0 0 0 0 0 0

- Change the first value of
`x`

to 100:

x[1] <- 100 x

## [1] 100 0 0 0 0 0

- To change multiple values at once, make sure the number of
**new values**equals the number of**selected values**:

x[c(1, 3, 5)] <- c(5, 5, 5) x

## [1] 5 0 5 0 5 0

x[4:6] <- x[4:6] + 1 x

## [1] 5 0 5 1 6 1

x[1:2] <- c(2) x

## [1] 2 2 5 1 6 1

x[1:2] <- c(100, 100, 100)

## Warning in x[1:2] <- c(100, 100, 100): number of items to replace is not a ## multiple of replacement length

x

## [1] 100 100 5 1 6 1

- You can also create values that do not yet exist in your object. R will expand the object to accomodate the new values.

x[7] <- 40 x

## [1] 100 100 5 1 6 1 40

- We can also do the same things to matrix, array, list or data frame!

deck

deck$new_column <- 1:52 deck

- We can also remove a column using the assigning them
`NULL`

.

deck$new_column <- NULL deck

- Let’s say in a certain game, aces receive the highest value of all the cards, say
`14`

. Modify the values in`deck`

to reflect this rule. - We can do this using the row indexes (numbers) of the aces.

deck[c(13, 26, 39, 52), ]

deck[c(13, 26, 39, 52), 3] <- 14 deck

deck[c(13, 26, 39, 52), ]

- We can do this in an easier method that will be covered next!

Operator | Syntax | Tests |
---|---|---|

`>` |
`a > b` |
Is `a` greater than `b` ? |

`>=` |
`a >= b` |
Is `a` greater than or equal to `b` ? |

`<` |
`a < b` |
Is `a` less than `b` ? |

`<=` |
`a <= b` |
Is `a` less than or equal to `b` ? |

`==` |
`a == b` |
Is `a` equal to `b` ? |

`!=` |
`a != b` |
Is `a` not equal to `b` ? |

`%in%` |
`a %in% c(a, b, c)` |
Is `a` in the group `c(a, b, c)` ? |

1 > 2

## [1] FALSE

1 > c(0, 1, 2)

## [1] TRUE FALSE FALSE

c(1, 2, 3) == c(3, 2, 1)

## [1] FALSE TRUE FALSE

1 %in% c(3, 4, 5)

## [1] FALSE

**Note**:`%in%`

is the only operator that does NOT follow element-wise execution.`%in%`

independently test whether each value on the left is*somewhere*in the vectore on the right.

c(1, 2) %in% c(3, 4, 5)

## [1] FALSE FALSE

c(1, 2, 3) %in% c(3, 4, 5)

## [1] FALSE FALSE TRUE

c(1, 2, 3, 4) %in% c(3, 4, 5)

## [1] FALSE FALSE TRUE TRUE

**Note**:`=`

is an assigment operator, like`<-`

.- Make sure not to confuse between
`=`

and`==`

.

a <- 6 b <- 0 a == b

## [1] FALSE

a = b a

## [1] 0

- Let’s say in a certain game, aces receive the highest value of all the cards, say
`14`

. - Modify the values in
`deck`

to reflect this rule. - First, what are the values of the aces in
`deck`

right now?

deck[deck$face == "ace", ]

deck$face

## [1] king queen jack ten nine eight seven six five four three two ## [13] ace king queen jack ten nine eight seven six five four three ## [25] two ace king queen jack ten nine eight seven six five four ## [37] three two ace king queen jack ten nine eight seven six five ## [49] four three two ace ## Levels: ace eight five four jack king nine queen seven six ten three two

deck$face == "ace"

## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ## [13] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ## [25] FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ## [37] FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE ## [49] FALSE FALSE FALSE TRUE

deck[deck$face == "ace", ]

- Now, let’s update the value for the aces to be
`14`

!

deck[deck$face == "ace", c("value")] <- 14 deck

deck[deck$face == "ace", ]

Operator | Syntax | Tests |
---|---|---|

`&` |
`cond1 & cond2` |
Are both `cond1` and `cond2` true? |

`|` |
`cond1 | cond2` |
Are `cond1` or `cond2` or both true? |

`xor` |
`xor(cond1, cond2)` |
Is exactly one of `cond1` and `cond2` true? |

`!` |
`!cond1` |
Is `cond1` false? (e.g., `!` flips the results of a logical test) |

`any` |
`any(cond1, cond2, cond3, ...)` |
Are any of the conditions true? |

`all` |
`all(cond1, cond2, cond3, ...)` |
Are all of the conditions true? |

- To use Boolean operator, place it between two
*complete*logical tests (logical statements).

- When used with vectors, Boolean operators will follow the same element-wise execution as arithmetic and logical operators.

a <- c(1, 2, 3) b <- c(1, 2, 3) c <- c(1, 2, 4) a == b

## [1] TRUE TRUE TRUE

b == c

## [1] TRUE TRUE FALSE

a == b & b == c

## [1] TRUE TRUE FALSE

- In a new game called
**hearts**, every card has value of`0`

except cards in the suit of hearts and the queen of spades.- The suit of hearts all have values of
`1`

. - The queen of spades has a value of
`13`

.

- The suit of hearts all have values of
- Load the data in
`cards.csv`

- URL: https://nkha149.github.io/stat385-sp2020/files/data/cards.csv
- Save data into a data frame named
`hearts_deck`

.

**Bring laptop to lecture on Wednesday!**We will have a lab on data frame and modifying values then.- Read Chapter 5: Modifying Values in
*Hands-On Programming with R*if you haven’t done so. - Read Chapter 7: Programs in
*Hands-On Programming with R*.

*Hands-On Programming with R*, by Garrett Grolemund.