# r toString: How to Create Character String in R

R has no built-in string data type. Instead, it has a character, and you can use the is.character() function to check. Type conversions in R work as you would expect. For example, adding the character string to a numeric vector converts all the vector items to the character.

## r toString

The toString() is a built-in R function that produces a single character string describing an R object. The toString() function takes an R object and returns a character vector of length 1. The toString() function converts an object to a character string. First, it converts an object to a character type, and then if you have passed the second argument, it will concatenate with it.

### Syntax

``toString(x, width = NULL, ...)``

### Parameters

x: It is an R object.

width: It is a maximum field width. Values of NULL or 0 indicate no maximum. The minimum value accepted is 6, and smaller values are taken as 6.

### Return Value

The toString() function returns a character vector of length 1.

### Example

Let’s define a double vector and then use the toString() function to convert it into a string. In the R sense, a character.

``````dt <- c(1, 2, 3)
typeof(dt)
ds <- toString(dt)
typeof(ds)``````

#### Output

``````[1] "double"
[1] "character"``````

As you can see that the toString() function converts a double to a string.

## Converting Matrix to String in R

To create a matrix in R, use the matrix() function. Then, let’s apply a toString() function to the matrix and convert the matrix into a string.

``````mt <- matrix(c(1:9), 3, 3)
typeof(mt)
mt
ds <- toString(mt)
typeof(ds)
ds``````

#### Output

``````[1] "integer"
[,1] [,2] [,3]
[1,]  1    4    7
[2,]  2    5    8
[3,]  3    6    9

[1] "character"
[1] "1, 2, 3, 4, 5, 6, 7, 8, 9"``````

That is it for the R toString() method.

## See also

agrep() function in R

chartr() function in R

substr() function in R