Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

R Type Conversion


Share

Type Conversion

Sometimes you need to change a value from one data type to another. This is called type conversion.

For example, numbers that come from a file or a form are often stored as text. A number in quotes is a string, so R cannot do math with it:

Example

age <- "25"

age + 1

Result:

Error in age + 1 : non-numeric argument to binary operator
Try it Yourself »

To get the right result, you need to know how type conversion works.

There are two types of conversion in R:

  • Implicit conversion (automatically)
  • Explicit conversion (manually)

Implicit Conversion

R converts some values automatically, when you combine different types in a calculation.

If you add an integer and a numeric value, R converts the result to numeric, so the decimals are not lost:

Example

x <- 5L + 2.5

x
class(x)
Try it Yourself »

Logical values are also converted automatically: TRUE becomes 1, and FALSE becomes 0:

Example

TRUE + 5
FALSE + 5
Try it Yourself »

Note: R never converts a string to a number automatically. That is why "25" + 1 gives an error. You must convert it yourself.


Explicit Conversion

To convert a value yourself, use one of these functions:

Function Converts to
as.numeric()numeric (a number, with or without decimals)
as.integer()integer (a whole number)
as.character()character (a string)
as.logical()logical (TRUE or FALSE)

Now we can fix the problem from the start of this page:

Example

age <- "25"

age <- as.numeric(age)

age + 1
Try it Yourself »


Convert Between Number Types

You can also convert between the number types from the R Numbers chapter:

Example

x <- 1L # integer
y <- 2 # numeric

# convert from integer to numeric:
a <- as.numeric(x)

# convert from numeric to integer:
b <- as.integer(y)

# print values of x and y
x
y

# print the class name of a and b
class(a)
class(b)
Try it Yourself »

Decimals Are Removed

Be careful when you convert a number with decimals to an integer. as.integer() does not round the number. It just removes the decimals:

Example

x <- 9.99

as.integer(x)
round(x)
Try it Yourself »

What happened to .99? It is gone! If you want the nearest whole number, use round() from the R Math chapter instead.


Convert to a String

Use as.character() to convert a number to a string:

Example

x <- 10

y <- as.character(x)

y
class(y)
Try it Yourself »

The result has quotes around it, because it is now text.


When Conversion Fails

If R cannot convert a value, it gives NA (a missing value) and shows a warning:

Example

x <- as.numeric("hello")

Result:

Warning message:
NAs introduced by coercion
Try it Yourself »

You will learn more about NA in the R Missing Values chapter, and about warnings in the R Errors chapter.


Real-Life Example

A web shop stores the price and the quantity as text. To find the total price, convert both values to numbers first:

Example

price <- "19.99"
quantity <- "3"

total <- as.numeric(price) * as.numeric(quantity)

total
Try it Yourself »


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.