Changing Values in Place

  • 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.

Atomic Vector

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

Atomic Vector

  • 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

Atomic Vector

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!

Data Frame

deck

Data Frame

deck$new_column <- 1:52
deck

Data Frame

  • We can also remove a column using the assigning them NULL.
deck$new_column <- NULL
deck

Data Frame

  • 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), ]

Data Frame

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

Data Frame

deck[c(13, 26, 39, 52), ]
  • We can do this in an easier method that will be covered next!

Logical Operators

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)?

Logical Operators

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

Logical Operators

  • 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

Logical Operators

  • 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

Using Logical Operator for Subsetting

  • 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", ]

Using Logical Operator for Subsetting

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

Using Logical Operator for Subsetting

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

Using Logical Operator for Subsetting

  • Now, let’s update the value for the aces to be 14!
deck[deck$face == "ace", c("value")] <-  14
deck

Using Logical Operator for Subsetting

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

Boolean Operators

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?

Boolean Operators

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

Boolean Operators

  • 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

Exercise

  • 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.
  • Load the data in cards.csv

To-do

References