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 –
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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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 –
- Convert Character to Numeric in R (With Examples)
- R – Count Occurrences of a Value in a Vector
- Check if an Element is present in an R Vector
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.