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
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)|
Character vector can have the following values
- A string of any length. for example
- Empty String “”
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
# 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 # "" "" "" "" "" # 5
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
# Create vector v <- c('a','ABC','bc','A',NA) print(v) typeof(v) # Output # "a" "ABC" "bc" "A" NA # "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
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 # "character"
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 3 2 1 NA
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  TRUE FALSE FALSE FALSE NA
In this article, I have explained what is character vector in R, character functions, and how to use them. character() function initialized character vector with 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.
- 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?
- Create Character 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?