R Programming: Learn R Programming In A DAY! - The Ultimate Crash Course to Learning the Basics of R Programming Language In No Time (R, R Programming, ... Course, R Programming Development Book 1) by Acodemy

R Programming: Learn R Programming In A DAY! - The Ultimate Crash Course to Learning the Basics of R Programming Language In No Time (R, R Programming, ... Course, R Programming Development Book 1) by Acodemy

Author:Acodemy [Acodemy]
Language: eng
Format: epub
Published: 2015-02-14T18:30:00+00:00


Notice in the above example that short lists are recycled, therefore c(“A”, “B”) is repeated five time to match the sequence 1:10.

Logical Vectors

R allows the manipulation of logical vectors. The elements of logical vectors may have TRUE, FALSE, and Not Available (NA) values. The TRUE and FALSE values are often abbreviated to T and F respectively. They are short form variables that are set to TRUE and FALSE, by default. They are not reserved words and therefore can be overwritten by the user. Therefore, it is better to write the full words, TRUE and FALSE.

Logical vectors are created by conditions, as you shown in the following example:

> temp <- x > 13

The above statement sets the “temp” variable as a vector with same length as “x” with FALSE values that correspond to the elements of x. This is where the condition is FALSE. The logical operators (<, <=, >, >=, ==) are used for equality and the != operator for not equal. Additionally, if v1 and v2 are logical expressions, then (!) operator is used.

Logical vectors can be used in ordinary arithmetic, where they are forced as numeric vectors and FALSE becomes 0 and TRUE becomes 1. There are certain logical vectors that have related numbers that are not equal.

Typically, the first step to establishing a vector is to define a vector with data, and the second step is to define a vector with logical values. When logical values are used for the index into the vector of data values, only the elements that correspond to the variables that are set to TRUE are returned. The following example will show you how it works:

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

> y <- c(TRUE,FALSE,FALSE,TRUE,FALSE)

> x[y] [1] 1 4



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.