Character Vector in R is a vector of a type character that is used to store strings and NA values. In this article, I will explain what is a character vector, how to use it with methods like character(), as.character() and is.character() functions.
A vector where each element has only alphabet characters (a-z or A-Z) of any size is a character vector. For example, c('ABC','abc','AbC','12AB') is a character vector of length 3. One can create a character vector using c() or character() functions.
Following are the most used methods of character vector in R and their usage.
| Character Vector Functions | Usage | | --- | --- | | character() | character(length = 0) | | is.character() | is.character(x) | | as.character() | as.character(x, ...) | | nchar() | |
Character vector can have the following values
- A string of any length. for example
a,ABC,abc,AbC - Empty String ""
- NA
<a id="h-1-character"></a>
1. character()
character() function is used to create a character vector in R of a specified length. All elements of the R vector will contain the empty string "". By default, not using a length creates an empty vector.
Following is the syntax of the character() vector.
# Syntax of character
character(length = 0)
The following example initializes a vector with 5 empty strings.
# Usage of character()
v <- character(4)
print(v)
length(v)
# Output
#[1] "" "" "" "" ""
#[1] 5
<a id="h-2-create-character-vector"></a>
2. Create Character Vector
To create a vector use c() function and to create a character vector use string elements as values. Once the vector is created, check the type of the vector using typeof() function.
# Create vector
v <- c('a','ABC','bc','A',NA)
print(v)
typeof(v)
# Output
#[1] "a" "ABC" "bc" "A" NA
#[1] "character"
<a id="h-3-is-character"></a>
3. is.character()
To check if the vector is of type character use is.character() function. This function takes a vector as an argument and returns TRUE for a character vector otherwise FALSE.
# Check if vector is character type
is.character(v)
# Output
# TRUE
# Numeric vector
n <- c(1,2,3)
is.character(n)
# Output
# FALSE
<a id="h-4-as-character"></a>
4. as.character()
as.character() function is used to convert the vector to the character type. This function takes the values of non-character and creates a character vector.
# Convert elements to character vector
v <- as.character(1,2,3)
typeof(v)
# Output
#[1] "character"
<a id="h-5-ncahr"></a>
5. ncahr()
The nchar() function is used to get the length of each element of the character vector in R. For the NA element it returns NA as length.
# Get length of each element.
v <- c('a','ABC','bc','A',NA)
nchar(v)
# Output
#[1] 1 3 2 1 NA
<a id="h-6-equal-condition"></a>
6. Equal Condition
When you check if a value is equal in a vector by an equal condition, it returns a logical vector representing TRUE if a value is equal otherwise represents FALSE.
res <- v == 'a'
print(res)
# Output
[1] TRUE FALSE FALSE FALSE NA
<a id="h-7-conclusion"></a>
7. Conclusion
In this article, I have explained what is character vectors in R, what character functions, and how to use them. character() function initialized character vector with an empty string. is.character() check if a vector is a character type. as.character() returns a character vector for non char elements. You can find the complete example from this article at Github R Programming Examples Project.
<a id="h-related-articles"></a>
Related Articles
- How to Get Vector Length in R?
- Add or Append Element to Vector in R?
- How to Remove NA from Vector?
- How to Create a Vector in R?
- How to Create a DataFrame From Vectors?
- How to Create Empty Vector in R?
- How to Convert Vector to List in R?
- How to Convert List to Vector in R?
- How to Concatenate Vector in R?
- Merge Vector Elements into String in R?
- How to Subset Vector in R?
- How to Sort Vector in R?
- How to Convert List to String?
- How to create numeric vector in R?
- Explain merge() function in R
<a id="h-references"></a>
