Modifying Values in R

Ha Khanh Nguyen

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

## [1] 0 0 0 0 0 0
  • Change the first value of x to 100:
## [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:
## [1] 5 0 5 0 5 0
## [1] 5 0 5 1 6 1
  • What happens otherwise?
## [1] 2 2 5 1 6 1
## Warning in x[1:2] <- c(100, 100, 100): number of items to replace is not a
## multiple of replacement length
## [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.
## [1] 100 100   5   1   6   1  40
  • We can also do the same things to matrix, array, list or data frame!

Matrix

##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
##      [,1] [,2] [,3] [,4]
## [1,]  100    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12

Array

## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    7    9   11
## [2,]    8   10   12
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  100    3    5
## [2,]    2    4    6
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    7    9   11
## [2,]    8   10   12

List

## [[1]]
## [[1]][[1]]
## [1] "Ha"
## 
## [[1]][[2]]
## [1] 24
## 
## 
## [[2]]
## [[2]][[1]]
## [[2]][[1]][[1]]
## [1] "Alex"
## 
## [[2]][[1]][[2]]
## [1] 20
## 
## 
## [[2]][[2]]
## [[2]][[2]][[1]]
## [1] "Dave"
## 
## [[2]][[2]][[2]]
## [1] 21
## [[1]]
## [[1]][[1]]
## [1] "Ha"
## 
## [[1]][[2]]
## [1] 24
## 
## 
## [[2]]
## [[2]][[1]]
## [[2]][[1]][[1]]
## [1] "Alex"
## 
## [[2]][[1]][[2]]
## [1] 21
## 
## 
## [[2]][[2]]
## [[2]][[2]][[1]]
## [1] "Dave"
## 
## [[2]][[2]][[2]]
## [1] 21

Data Frame

  • We can also remove a column using the assigning them NULL.
  • 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.
  • We can do this in an easier method that will be covered next!

Logical Subsetting

Logical Tests

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] FALSE
## [1]  TRUE FALSE FALSE
## [1] FALSE  TRUE 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.
## [1] FALSE
## [1] FALSE FALSE
## [1] FALSE FALSE  TRUE
## [1] FALSE FALSE  TRUE  TRUE
  • Note: = is an assigment operator, like <-. Make sure not to confuse between = and ==.
## [1] FALSE
## [1] 0
## [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?
  • How does the above code work?
##  [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
##  [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
  • Now, let’s update the value for the aces to be 14!

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?
  • 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.
## [1] TRUE TRUE TRUE
## [1]  TRUE  TRUE FALSE
## [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 and the queen of spades has a value of 13.
  • First, load the data in cards.csv from the URL: https://nkha149.github.io/stat385-sp2020/files/data/cards.csv into a data frame named hearts_deck.
  • Change the values for all cards on deck to 0.
  • Change the value for cards in the hearts suit to 1.
  • Change the value of the queen of spades to 13.
  • Take a final look at hearts_deck:

To-do

References