0
0
R Programmingprogramming~5 mins

Numeric type in R Programming

Choose your learning style9 modes available
Introduction

Numeric types let you work with numbers in your program. They help you do math and store values like age, price, or temperature.

When you want to add, subtract, multiply, or divide numbers.
When you need to store measurements like height or weight.
When you want to count items or calculate averages.
When you want to compare numbers to make decisions.
When you want to use numbers in graphs or charts.
Syntax
R Programming
x <- 5       # Assign a number to a variable
class(x)      # Check the type of the variable
In R, numbers are usually stored as numeric type by default.
You can use integers by adding L after the number, like 5L.
Examples
This shows that both integers and decimals are numeric types in R.
R Programming
a <- 10
b <- 3.14
class(a)
class(b)
The L makes the number an integer type, which is a kind of numeric type.
R Programming
c <- 7L
class(c)
You can convert text to numeric using as.numeric().
R Programming
d <- as.numeric("12.5")
class(d)
Sample Program

This program adds two numeric values and prints the result and their types.

R Programming
x <- 8
pi_val <- 3.14159
sum_val <- x + pi_val
print(sum_val)
print(class(x))
print(class(pi_val))
OutputSuccess
Important Notes

R treats numbers with decimals as numeric (double precision) by default.

Use is.numeric() to check if a value is numeric.

Integer numbers can be created by adding L after the number, like 5L.

Summary

Numeric types store numbers for calculations and measurements.

R uses numeric for decimals and integer for whole numbers with L.

You can convert text to numbers with as.numeric().