R Numbers
R Numbers
Tutorial Name: Codes With Pankaj Website: www.codeswithpankaj.com
Table of Contents
Introduction to Numbers in R
Types of Numbers in R
Numeric (Floating-Point Numbers)
Integer
Complex Numbers
Basic Arithmetic Operations
Mathematical Functions
Rounding Numbers
Trigonometric Functions
Logarithmic Functions
Generating Sequences of Numbers
Random Number Generation
Handling Special Numbers
NA (Not Available)
NaN (Not a Number)
Inf (Infinity)
Coercion of Numbers
1. Introduction to Numbers in R
Numbers are a fundamental data type in R. R can handle various types of numbers, including integers, floating-point numbers (real numbers), and complex numbers. These numbers can be used for mathematical calculations, data analysis, and statistical modeling.
Example:
2. Types of Numbers in R
2.1 Numeric (Floating-Point Numbers)
Numeric is the default data type for numbers in R, representing real numbers with decimal points.
Example:
2.2 Integer
Integers are whole numbers without decimal points. You can explicitly define an integer by adding an L
suffix to the number.
Example:
2.3 Complex Numbers
Complex numbers consist of a real and an imaginary part. In R, complex numbers are represented with the format a + bi
, where i
is the imaginary unit.
Example:
3. Basic Arithmetic Operations
R provides basic arithmetic operations for numbers, including addition, subtraction, multiplication, division, exponentiation, and modulus.
Example:
4. Mathematical Functions
R has a variety of built-in mathematical functions that allow you to perform complex calculations.
4.1 Rounding Numbers
round()
: Rounds to the nearest whole number or specified decimal place.ceiling()
: Rounds up to the nearest integer.floor()
: Rounds down to the nearest integer.
Example:
4.2 Trigonometric Functions
sin()
,cos()
,tan()
: Trigonometric functions in R.
Example:
4.3 Logarithmic Functions
log()
: Natural logarithm (basee
).log10()
: Logarithm with base 10.
Example:
5. Generating Sequences of Numbers
You can create sequences of numbers in R using the :
operator or the seq()
function.
Example:
6. Random Number Generation
R provides functions to generate random numbers for various distributions, such as uniform and normal distributions.
runif()
: Generates random numbers from a uniform distribution.rnorm()
: Generates random numbers from a normal distribution.
Example:
7. Handling Special Numbers
7.1 NA (Not Available)
NA
represents missing values in R. Operations with NA
typically return NA
.
Example:
7.2 NaN (Not a Number)
NaN
represents undefined mathematical operations, such as dividing zero by zero.
Example:
7.3 Inf (Infinity)
Inf
represents positive or negative infinity in R, typically resulting from division by zero.
Example:
8. Coercion of Numbers
R allows you to convert between different types of numbers using coercion functions like as.numeric()
, as.integer()
, and as.complex()
.
Example:
Conclusion
Understanding numbers and their operations in R is essential for performing calculations and data analysis. Whether you're working with basic arithmetic, generating random numbers, or handling special values, R provides powerful tools for numerical computations.
For more tutorials and resources, visit Codes With Pankaj at www.codeswithpankaj.com.
Last updated