2.12 Logical Vectors: Create with Repetitions and Summary Functions

Whenever you need to create repeated vectors that are of logical type, you can conveniently achieve such tasks through methods you learned in @ref(patterned_numeric_vectors_creation) and 2.9.

2.12.1 Create logical vectors with repetitions

Let’s start by recalling how we create numeric and character vectors with repetitions. The function we use is the rep() function, inside of which the first argument refers to elements to be repeated, and the second argument indicates the number of repetitions.

num1 <- rep(1, 3)
cha1 <- rep("sheep", 2)

Remember that, the first argument can be 1) a number, 2) a character, 3) colon operator : (for numeric vectors), 4) a numeric vector with several values, and 5) a character vector with several values; the second argument can be 1) a number, 2) colon operator :, and 3) a numeric vector with several values. When only the first argument is a vector with several values, it is repeated as a whole, not elementwisely. When both arguments are vectors with several values, R will do an element repeat operation.

num2 <- rep(c(1, 0), 3)
num3 <- rep(1:3, 3:1)
num4 <- rep(c(1, 0), c(2, 2))
char2 <- rep(c("apple", "chocolate"), 2)
char3 <- rep(c("apple", "chocolate"), 1:2)
char4 <- rep(c("apple", "chocolate"), c(3, 3))

If you feel confident creating numeric and character vectors with repetitions, the ways of creating logical vectors with repetitions are basically at your fingertips:

logic <- rep(c(TRUE, FALSE, NA), c(3, 2, 1))
logic

2.12.2 Statistical Functions on Logical Vectors

Before we end our study of logical vectors with logical operators in next section, we will take a look at how some previously introduced summary functions can be used on logical vectors.

Group A: summary statistics

What if we apply summary() on logical vectors?

logic <- rep(c(T, F, T, NA), 4:1)
summary(logic)
#>    Mode   FALSE    TRUE    NA's 
#> logical       3       6       1

Similar to character vectors, you can get the vector type, which is logical here. You also get a frequency table for the times FALSE, TRUE, and missing values NA appear in the vector.

Group B: arithmetic operations

Different from character vectors, logical vectors can be used in ordinary arithmetic, meaning you can apply almost all the functions summarized in Table ?? to logical vectors. In such cases, the coercion rule introduced in Section 2.4 will convert every TRUE to 1 and FALSE to 0. Let’s take a look at an example,

a <- c(TRUE, TRUE, FALSE, FALSE, TRUE)
sum(a)
#> [1] 3
mean(a)
#> [1] 0.6

Clearly, to proceed calculations, R considers a having three 1s and two 0s, given there are three TRUE and two FALSE. Therefore, sum(a)equals 3 andmean(a)` equals 0.6. You are welcome to try other functions on a logical vector.

However, as much as TRUE and FLASE will be coerced into specific numeric values, missing values NA won’t be coerced to an assigned value. Therefore, ordinary arithmetic involving NA will return NA as well, meaning the result is not available.

b <- c(TRUE, FALSE, TRUE, NA)
sum(b)
#> [1] NA
mean(b)
#> [1] NA
d <- c(1:5, NA)
sum(d)
#> [1] NA
e <- c(1:5, NaN)
sum(e)
#> [1] NaN
f <- c(1:5, NA, NaN)
is.na(d)
#> [1] FALSE FALSE FALSE FALSE FALSE  TRUE
is.nan(d)
#> [1] FALSE FALSE FALSE FALSE FALSE FALSE

2.12.3 Exercises

  1. Write the R code to create logical vectors with the following results, then show the unique elements and their corresponding frequencies in each vector:

a).FALSE TRUE NA FALSE TRUE NA b). FALSE FALSE TRUE TRUE TRUE NA

  1. Write two different R codes using rep() function to create a logical vector with values TRUE FALSE TRUE FLASE. The two codes should have at least one argument different from each other.