How to create a character vector in R? Use character()
or c()
functions to create a character vector. character() creates a vector with a specified length of all empty strings whereas c() creates a vector with the specified values, if all values are strings then it creates a character vector.
A vector whose all elements are either string or NA values is called a character vector.
1. Quick Examples of Create Character Vector
The following are quick examples of how to create a character vector in R.
# Quick Examples
# Create Character Vector
v <- c('a1','ABC','bc','A',NA)
print(v)
typeof(v)
# Create Character Vector of empty Strings
v <- character(5)
length(v)
print(v)
# Create Empty Character Vector
v <- character()
2. Create Character Vector in R
c()
function is used to create a vector. By passing all elements with characters to c()
function it creates a character vector in R. Use typeof()
to check whether the created is a character vector or not.
# Create Character Vector
v <- c('a1','ABC','bc','A',NA)
print(v)
typeof(v)
Yields below output
# Output
> print(v)
[1] "a1" "ABC" "bc" "A" NA
> typeof(v)
[1] "character"
3. Create a Character Vector of Empty Strings
The character()
function creates a character vector in R of a specified length. All elements of the vector will contain the empty string “”. The following example initializes a character vector of length 5 with all empty strings.
# Create Character Vector of empty Strings
v <- character(5)
length(v)
print(v)
Yields below output
# Output
> length(v)
[1] 5
> print(v)
[1] "" "" "" "" ""
4. Empty Character Vector
character()
function by default creates an empty vector in R of type character. When no argument is used, it by default takes 0 to length param and creates zero length vector. typeof(character())
returns character
type.
# Create Empty Character Vector
v <- character()
print(v)
Yields below output.
# Output
> print(v)
character(0)
5. Convert to Character Vector
If you have a numeric vector, you can easily convert it to a character vector by using as.character()
function. as.character()
function is used to convert the vector of any type to the character type. This function takes the values of non-character and creates a character vector.
# Convert to Character Cector
v <- as.character(1,2,3)
typeof(v)
Yields below output
# Output
> typeof(v)
[1] "character"
Conclusion
In this article, you have learned how to create a character vector in R by using functions c()
and character()
. Function character()
creates a vector with the specified length of all empty string values whereas c()
initializes a vector with the specified values. You can find the complete example from this article at Github R Programming Examples Project.
Related Articles
- How to create numeric vector in R?
- Explain merge() function in R
- 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?