# Modifying Values in R

## 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
• 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
• What happens otherwise?
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!

### Matrix

# matrix
m <- matrix(data = 1:12, nrow = 3)
m
##      [,1] [,2] [,3] [,4]
## [1,]    1    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12
# modifying value in matrix
m[1, 1] <- 100
m
##      [,1] [,2] [,3] [,4]
## [1,]  100    4    7   10
## [2,]    2    5    8   11
## [3,]    3    6    9   12

### Array

# array
a <- array(data = 1:12, dim = c(2, 3, 2))
a
## , , 1
##
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
##
## , , 2
##
##      [,1] [,2] [,3]
## [1,]    7    9   11
## [2,]    8   10   12
# modifying value in array
a[1, 1, 1] <- 100
a
## , , 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

# list
instructor <- list("Ha", 24)
students <- list(list("Alex", 20), list("Dave", 21))
stat385 <- list(instructor, students)
stat385
## [[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
# modifying value in list
stat385[[2]][[1]][[2]] <- stat385[[2]][[1]][[2]] + 1
stat385
## [[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

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!

## 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 > 2
## [1] FALSE
1 > c(0, 1, 2)
## [1]  TRUE FALSE FALSE
c(1, 2, 3) == c(3, 2, 1)
## [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 %in% c(3, 4, 5)
## [1] FALSE
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
b
## [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", ] • How does the above code work? 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", ]

### 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.
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 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.
hearts_deck <- read.csv(file = "https://nkha149.github.io/stat385-sp2020/files/data/cards.csv")
hearts_deck
• Change the values for all cards on deck to 0.
hearts_deck$value <- 0 hearts_deck • Change the value for cards in the hearts suit to 1. hearts_deck[hearts_deck$suit == "hearts", ]
hearts_deck[hearts_deck$suit == "hearts", 3] <- 1 hearts_deck[hearts_deck$suit == "hearts", ]
• Change the value of the queen of spades to 13.
hearts_deck[hearts_deck$face == "queen" & hearts_deck$suit == "spades", ]
hearts_deck[hearts_deck$face == "queen" & hearts_deck$suit == "spades", 3] <- 13
hearts_deck[hearts_deck$face == "queen" & hearts_deck$suit == "spades", ]
• Take a final look at hearts_deck:
hearts_deck