# Create Character Vector in R

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)
 "a1"  "ABC" "bc"  "A"   NA
> typeof(v)
 "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)
 5
> print(v)
 "" "" "" "" ""
``````

## 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)
 "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.

## References

### NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more .. 