Logical and Character Vectors in R
By: Karthik Janar
The simplest and most common data structure in R is the vector. Vectors come in two different flavors: atomic vectors and lists. An atomic vector contains exactly one data type, whereas a list may contain multiple data types. Numeric vectors are one type of atomic vector. Other types of atomic vectors include logical, character, integer, and complex. In this tutorial, weâ€™ll take a closer look at logical and character vectors.
Logical vectors can contain the values TRUE, FALSE, and NA (for â€˜not availableâ€™). These values are generated as the result of logical â€˜conditionsâ€™. Letâ€™s experiment with some simple conditions.
First, create a numeric vector num_vect that contains the values 0.5, 55, -10, and 6.
num_vect <- c(0.5,55,-10,6)
Now, create a variable called tf that gets the result of num_vect < 1, which is read as â€˜num_vect is less than 1â€™.
tf <- num_vect < 1
This results in a vector of 4 logical values. Print the contents of tf now.
tf
## [1] TRUE FALSE TRUE FALSE
The statement num_vect < 1 is a condition and tf tells us whether each corresponding element of our numeric vector num_vect satisfies this condition.
The first element of num_vect is 0.5, which is less than 1 and therefore the statement 0.5 < 1 is TRUE. The second element of num_vect is 55, which is greater than 1, so the statement 55 < 1 is FALSE. The same logic applies for the third and fourth elements.
Letâ€™s try another. Type num_vect >= 6 without assigning the result to a new variable.
num_vect >= 6
## [1] FALSE TRUE FALSE TRUE
This time, we are asking whether each individual element of num_vect is greater than OR equal to 6. Since only 55 and 6 are greater than or equal to 6, the second and fourth elements of the result are TRUE and the first and third elements are FALSE.
The <
and >=
symbols in these examples are called â€˜logical operatorsâ€™. Other logical operators include >
, <=
, ==
for exact equality, and !=
for inequality.
If we have two logical expressions, A and B, we can ask whether at least one is TRUE with A | B (logical â€˜orâ€™ a.k.a. â€˜unionâ€™) or whether they are both TRUE with A & B (logical â€˜andâ€™ a.k.a. â€˜intersectionâ€™). Lastly, !A is the negation of A and is TRUE when A is FALSE and vice versa.
Itâ€™s a good idea to spend some time playing around with various combinations of these logical operators until you get comfortable with their use. Weâ€™ll do a few examples here to get you started.
(3 > 5) & (4 == 4)
## [1] FALSE
(TRUE == TRUE) | (TRUE == FALSE)
## [1] TRUE
((111 >= 111) | !(TRUE)) & ((4 + 1) == 5)
## [1] TRUE
Working with logical statements in R takes practice.
Character vectors are also very common in R. Double quotes are used to distinguish character objects, as in the following example.
Create a character vector that contains the following words: â€œMyâ€, â€œnameâ€, â€œisâ€. Remember to enclose each word in its own set of double quotes, so that R knows they are character strings. Store the vector in a variable called my_char and print its contents
my_char <- c("My", "name", "is")
my_char
## [1] "My" "name" "is"
Right now, my_char is a character vector of length 3. Letâ€™s say we want to join the elements of my_char together into one continuous character string (i.e.Â a character vector of length 1). We can do this using the paste() function.
paste(my_char, collapse = " ")
## [1] "My name is"
The collapse
argument to the paste() function tells R that when we join together the elements of the my_char character vector, weâ€™d like to separate them with single spaces.
To add (or â€˜concatenateâ€™) your name to the end of my_char, use the c() function like this: c(my_char, â€œyour_name_hereâ€). Store the result in a new variable called my_name.
my_name <- c(my_char, "Karthik")
my_name
## [1] "My" "name" "is" "Karthik"
Now, use the paste() function once more to join the words in my_name together into a single character string using collapse = " â€œ!
paste(my_name, collapse = " ")
## [1] "My name is Karthik"
In this example, we used the paste() function to collapse the elements of a single character vector. paste() can also be used to join the elements of multiple character vectors.
In the simplest case, we can join two character vectors that are each of length 1 (i.e.Â join two words). Try paste(â€œHelloâ€, â€œworld!â€, sep = " â€œ), where the sep
argument tells R that we want to separate the joined elements with a single space.
paste("Hello", "World!", sep = " ")
## [1] "Hello World!"
For a slightly more complicated example, we can join two vectors, each of length 3. Use paste() to join the integer vector 1:3 with the character vector c(â€œXâ€, â€œYâ€, â€œZâ€). This time, use sep = â€œâ€ to leave no space between the joined elements.
paste(c(1:3), c("X", "Y", "Z"), sep = "")
## [1] "1X" "2Y" "3Z"
What do you think will happen if our vectors are of different length? Vector recycling! Try paste(LETTERS, 1:4, sep = â€œ-â€), where LETTERS is a predefined variable in R containing a character vector of all 26 letters in the English alphabet.
paste(LETTERS, 1:4, sep = "-")
## [1] "A-1" "B-2" "C-3" "D-4" "E-1" "F-2" "G-3" "H-4" "I-1" "J-2" "K-3"
## [12] "L-4" "M-1" "N-2" "O-3" "P-4" "Q-1" "R-2" "S-3" "T-4" "U-1" "V-2"
## [23] "W-3" "X-4" "Y-1" "Z-2"
Since the character vector LETTERS is longer than the numeric vector 1:4, R simply recycles, or repeats, 1:4 until it matches the length of LETTERS. Also worth noting is that the numeric vector 1:4 gets â€˜coercedâ€™ into a character vector by the paste() function. Coercion here means that the numbers 1, 2, 3, and 4 in the output above are no longer numbers to R, but rather characters â€œ1â€, â€œ2â€, â€œ3â€, and â€œ4â€.
Archived Comments
Most Viewed Articles (in Data Science ) Discrete Vs Continuous Random Variables in R Functions in R - Creating your first R function Manipulating Data with dplyr in R Data Analytics - Which programming language to learn. R vs Python Introduction to logical operations in R Types of Data Science Questions? Logical and Character Vectors in R |
Latest Articles (in Data Science) |
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview