R Character Vectors

R Character Vector is an atomic vector whose type is “character”. A character vector can have strings or NA as values.

In this tutorial, we will learn how to create a character vector in R, with examples.

Allowed Values in Character Vector

The following are the list of values allowed in a character vector.

  • Strings
  • NA (Missing Value)

Create a Character Vector using c()

To create a character vector, we can use c() function.

In the following example, we create a character vector with length 5. We print the type of vector, and the vector contents.

example.R

</>
Copy
x <- c("a", "e", "i", "o", "u")
print(typeof(x))
print(x)

Output

[1] "character"
[1] "a" "e" "i" "o" "u"

A character vector can contain NA value as well along with strings.

In the following program, we create a character vector with some of the values as NA.

example.R

</>
Copy
x <- c("a", NA, NA, "o", "u")
print(typeof(x))
print(x)

Output

[1] "character"
[1] "a" NA  NA  "o" "u"

Conclusion

In this R Tutorial, we learned what character vectors are, different values allowed in a character vector, and how to create a character vector, with the help of examples.