Chapter 3 Look at Your Data! One Variable at a Time…

3.1 Types of Variables

3.1.1 Logical Operations

Logical evaluation is an important concept (and tool) in just about every computing language. R is no different. The basic idea is that we want to evaluate statements to determine whether the statement is TRUE or FALSE. Aside from messages indicating an error of some sort, these are the only two valid results: TRUE or FALSE.

3.1.1.1 Simple Logic Operators

The simplest logic operators are ==, <, <=, >, >=, and !. Except for ! (not), each of these operators requires an object on the left-hand-side (LHS) and an object on the right-hand-side (RHS).

For example, suppose we set a variable \(a\) to the value 3.

> a <- 3

We can evaluate whether \(a\) is equal to 3 as follows:

> a == 3
[1] TRUE

If, instead, we evaluate whether \(a\) is equal to 4, we receive a different result:

> a == 4
[1] FALSE

Be careful of confusing = with ==. The first, =, is an assignment operator — like the backarrow assignment operator <-. The second, ==, is a logical operator that determines whether the LHS is equal to the RHS.

When we are dealing with numeric values, we can evaluate whether one is less than or greater than the other. Again, we’ll use the example above, where \(a=3\).

> a > 2
[1] TRUE
> a > 3
[1] FALSE
> a >= 3
[1] TRUE
> a < 4
[1] TRUE
> a <= 2
[1] FALSE

If we want “strict” inequality, we use < or >. If the inequality is not strict, then we use <= (less than or equal to) or >= (greater than or equal to) for the evaluation.


Interactive Example: Simple Logical Evaluations

The following demonstrates simple logical evaluations. Each time you click the “Show Another” button, the app will assign new values for \(a\) and \(b\) and will evaluate four logical expressions. Keep refreshing the app until you feel like you understand the logical evaluations.


3.1.2 Logical Evaluation with Vectors

Using logical evaluations with vectors is not that different from what we just saw with scalar (individual) values. For logical operations on vectors, there are two common cases: (1) only one of the objects is a vector or (2) both objects — i.e., both the LHS and RHS — are vectors.

To illustrate the first case, let’s assume the variable \(x\) has been assigned the vector

> x <- c(4,1,3,2,5)
> x
[1] 4 1 3 2 5

Now, suppose we want to evaluate the expression \(x > 3\). In R, we write that exactly as we would have before

> x > 3
[1]  TRUE FALSE FALSE FALSE  TRUE

In this case, R compares each element of the vector \(x\) and returns whether that element is greater than 3. In this example, only 4 and 5 are strictly greater than 3, so only those positions in the output vector are TRUE. The positions corresponding to 1, 3, and 2 are FALSE.


Interactive Example: Logical Evaluation with a Vector and a Scalar

In the following example, a vector of numbers is assigned to the variable \(x\). The vector \(x\) is then compared to a scalar (single number) through a logical evaluation and the results are shown as a vector of TRUE/FALSE values. Keep refreshing the app until you understand how logical evaluations work when one object is a vector and one is a single number.


3.2 Measures of Centrality

3.2.1 Mean


Interactive Example


3.2.2 Median


Interactive Example


3.2.3 Mode


Interactive Example


3.3 Variance


Interactive Example


3.4 Simple Plots

3.4.1 Histogram

3.4.2 Density Plot

3.4.3 Box Plot

3.5 Summary Statistics

3.6 Missing Values


Interactive Example