R-Sessions 10: Conditionals

Conditionals, or logicals, are used to check vectors of data against conditions. In practice, this is used to select subsets of data or to recode values. Here, only some of the fundamentals of conditionals are described.

Basics

The general form of conditionals are two values, or two sets of values, and the condition to test against. Examples of such tests are ‘is larger than’, ‘equals’, and ‘is larger than’. In the example below the values ‘3’ and ‘4’ are tested using these three tests.

3 > 4 3 == 4 3 < 4

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

Numerical returns

The output shown directly above makes clear that R-Project returns the values ‘TRUE’ and ‘FALSE’ to conditional tests. The results here are pretty straightforward: 3 is not larger than 4, therefore R returns FALSE. If you don’t desire TRUE or FALSE as response, but a numeric output, use the as.numeric() command which transforms the values to numerics, in this case ‘0’ or ‘1’. This is shown below.

as.numeric(3 > 4) as.numeric(3 < 4)

> as.numeric(3 > 4)
[1] 0
> as.numeric(3 < 4)
[1] 1

Conditionals on vectors

As on most functionality of R-project, vectors (or multiple values) can be used alongside single values, as is the case on conditionals. These can be used not only against single values, but against variables containing multiple values as well. This will result in a succession of tests, one for each value in the variable. The output is a vector of values, ‘TRUE’ or ‘FALSE’.The examples below show two things: the subsequent values 1 to 10 are tested against the condition ‘is smaller than or equals 5′. It is shown as well that when these values are assigned to a variable (here: ‘x’), this variable can be tested against the same condition, giving exactly the same results.

1:10 1:10 <= 5 x <- 1:10 x <= 5

> 1:10
 [1]  1  2  3  4  5  6  7  8  9 10
> 1:10 <= 5
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE
> x <- 1:10
> x == 5
 [1]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE

Conditionals and multiple tests

More tests can be gathered into one conditional expression. For instance, building on the example above, the first row of the next example tests the values of variable ‘x’ against being smaller than or equal to 4, or being larger than or equal to ‘6’. This results in ‘TRUE’ for all the values, except for 5. Since the ‘|’-operator is used, only one of the set conditions need to be true. The second row of this example below tests the same values against two conditions as well, namely ‘equal to or larger than 4′ and ‘equal to or smaller than 6′. since this time the ‘&’-operator is used, both conditionals need to be true.

x <= 4 | x >= 6 x >= 4 & x <= 6

> x <= 4 | x >= 6
 [1]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
> x >= 4 & x <= 6
 [1] FALSE FALSE FALSE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE

Conditionals on character values

In the example below, a string variable ‘gender’ is constructed, containing the values ‘male’ and ‘female’. This is shown in the first two rows of the example below.

gender <- c(“male”,”female”,”female”,”male”,”male”,”male”,”female”) gender == “male”

> gender <- c("male","female","female","male","male","male","female")
> gender == "male"
[1]  TRUE FALSE FALSE  TRUE  TRUE  TRUE FALSE

Additional functions

The last examples demonstrate two other functions using conditionals, using the same ‘gender’ variable as above. The first is an additional way to get a numerical output of the same test as in the row above. The iselse() command has three arguments: the first is a conditional, the second is the desired output if the conditional is TRUE, the third is the output in case the result of the test is ‘FALSE’. The second example shows a way to obtain a list of which values match the condition tested against. In the output above, the second, third and last values are ‘female’. Using which() and the condition “== ‘male’ ” (equals ‘male’) returns the indices of the values in variable ‘gender’ that equal ‘male’.

ifelse(gender==”male”,0,1) which(gender==”male”)

> ifelse(gender=="male",0,1)
[1] 0 1 1 0 0 0 1
> which(gender=="male")
[1] 1 4 5 6

Conditionals on missing values

Missings values (‘NA’) form a special case in many ways, such as when using conditionals. Normal conditionals cannot be used to find the missing values in a range of values, as is shown below.

x <- c(4,3,6,NA,4,3,NA) x == NA which(x == NA) is.na(x) which(is.na(x))

The last two rows of the syntax above show what can be done. The is.na() command tests whether a value or a vector of values is missing. It returns a vector of logicals (‘TRUE’ or ‘FALSE’), that indicates missing values with a ‘TRUE’. Nesting this command in the which() command described earlier enables us to find which of the values are missing. In this case, the fourth and the seventh values are missing.

> x <- c(4,3,6,NA,4,3,NA)
> x == NA
[1] NA NA NA NA NA NA NA
> which(x == NA)
integer(0)
> is.na(x)
[1] FALSE FALSE FALSE  TRUE FALSE FALSE  TRUE
> which(is.na(x))
[1] 4 7


– – — — —– ——–

– – — — —– ——–
R-Sessions is a collection of manual chapters for R-Project, which are maintained on Curving Normality. All posts are linked to the chapters from the R-Project manual on this site. The manual is free to use, for it is paid by the advertisements, but please refer to it in your work inspired by it. Feedback and topic requests are highly appreciated.
——– —– — — – –

One comment on “R-Sessions 10: Conditionals

Leave a Reply