Page 28 - Applied Statistics with R
P. 28
28 CHAPTER 3. DATA AND PROGRAMMING
x[x > 3]
## [1] 5 7 8 9
x[x != 3]
## [1] 1 5 7 8 9
• TODO: coercion
sum(x > 3)
## [1] 4
as.numeric(x > 3)
## [1] 0 0 1 1 1 1
Here we see that using the sum() function on a vector of logical TRUE and FALSE
values that is the result of x > 3 results in a numeric result. R is first auto-
matically coercing the logical to numeric where TRUE is 1 and FALSE is 0. This
coercion from logical to numeric happens for most mathematical operations.
which(x > 3)
## [1] 3 4 5 6
x[which(x > 3)]
## [1] 5 7 8 9
max(x)
## [1] 9
which(x == max(x))
## [1] 6

