i

R Programming Complete Tutorial

Creating Character Strings

R provides the function character() to create character vectors. More specifically, using the character() function, we can create "character" type vector objects.When using the character(), we just have to specify the length of the vector. It will generate a character vector filled with empty strings.As with any other vector, once an empty character vector has been created, by simply giving it an index value outside its previous range, we may add new components to that.

Function c()

  • There is also the generic function c() that we can use to create character vectors. Simply pass any number of character elements separated by commas

  • We can use single or double quotes to define the character elements inside c().

    is.character() and as.character()

  • These two functions are methods for coercing objects to type "character", and testing whether an R object is of type "character".

  • The function as.character() in R allows us to convert non-character objects into character strings.