# Convert Numeric Type to Character Type in R

In this tutorial, we will look at how to convert a numeric type value (or collection) to character type in R with the help of some examples.

## How to convert numeric type to character in R?

You can use the `as.character()` function in R to convert numeric type to character type in R. Pass the field (for example, a vector) as an argument to the function. The following is the syntax –

Introductory ⭐

Intermediate ⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

`as.character(x)`

If you pass a vector to the above function, it returns a vector with each value converted to the character type. You can also use this function on dataframe columns in R (see the examples below).

## Examples

Let’s now look at some examples of using the `as.character()` function in R.

### Convert numeric type vector to character type vector

Let’s create a vector with numbers and then apply the `as.character()` function to convert them to character type values.

```# create a vector
vec <- c(1, 2, 3, 4)
# convert to character type
vec <- as.character(vec)
# display the vector
print(vec)
# display vector's type
print(class(vec))```

Output:

``` "1" "2" "3" "4"
 "character"```

You can see that the values in the resulting vector are of character type.

### Convert numeric type dataframe column to character type

You can also use the `as.character()` function to change the data type of a dataframe column in R to the character type.

Let’s look at an example.

First, we will create a dataframe with the height and weight information of some students in a university.

```# create a data frame
df <- data.frame(
"Height"= c(168, 174, 162, 158),
"Weight"= c(73, 81, 65, 55)
)

# display the dataframe
print(df)```

Output:

```   Name Height Weight
1   Tim    168     73
2 Hasan    174     81
4 Maria    158     55```

You can see that the “Height” and “Weight” columns in the above dataframe are of numeric type. Let’s change the type of the “Height” column to the character type using the `as.character()` function.

```# convert "Height" column to character type
df\$Height <- as.character(df\$Height)

# display "Height" column's type
print(class(df\$Height))```

Output:

` "character"`

You can see that the “Height” column is now of character data type.

You might also be interested in –

• 