convert numeric type vector to character type in R

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?

convert numeric type vector to character type 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 –

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] "1" "2" "3" "4"
[1] "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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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(
  "Name"= c("Tim", "Hasan", "Vlad", "Maria"),
  "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
3  Vlad    162     65
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:

[1] "character"

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

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top