R Numbers

R Numbers

Tutorial Name: Codes With Pankaj Website: www.codeswithpankaj.com


Table of Contents

  1. Introduction to Numbers in R

  2. Types of Numbers in R

    • Numeric (Floating-Point Numbers)

    • Integer

    • Complex Numbers

  3. Basic Arithmetic Operations

  4. Mathematical Functions

    • Rounding Numbers

    • Trigonometric Functions

    • Logarithmic Functions

  5. Generating Sequences of Numbers

  6. Random Number Generation

  7. Handling Special Numbers

    • NA (Not Available)

    • NaN (Not a Number)

    • Inf (Infinity)

  8. 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:

# Numeric and integer examples
num <- 10.5  # Numeric (floating-point)
int <- 7L  # Integer

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:

x <- 15.75
print(class(x))  # Output: "numeric"

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:

y <- 10L
print(class(y))  # Output: "integer"

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:

z <- 3 + 4i
print(class(z))  # Output: "complex"

3. Basic Arithmetic Operations

R provides basic arithmetic operations for numbers, including addition, subtraction, multiplication, division, exponentiation, and modulus.

Example:

a <- 10
b <- 3

sum <- a + b  # Addition
diff <- a - b  # Subtraction
prod <- a * b  # Multiplication
quot <- a / b  # Division
power <- a ^ b  # Exponentiation
mod <- a %% b  # Modulus

print(sum)  # Output: 13
print(diff)  # Output: 7
print(prod)  # Output: 30
print(quot)  # Output: 3.333
print(power)  # Output: 1000
print(mod)  # Output: 1

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:

num <- 3.567
print(round(num, 2))  # Output: 3.57
print(ceiling(num))  # Output: 4
print(floor(num))  # Output: 3

4.2 Trigonometric Functions

  • sin(), cos(), tan(): Trigonometric functions in R.

Example:

angle <- pi / 4
print(sin(angle))  # Output: 0.707
print(cos(angle))  # Output: 0.707
print(tan(angle))  # Output: 1

4.3 Logarithmic Functions

  • log(): Natural logarithm (base e).

  • log10(): Logarithm with base 10.

Example:

num <- 100
print(log(num))  # Output: 4.605
print(log10(num))  # Output: 2

5. Generating Sequences of Numbers

You can create sequences of numbers in R using the : operator or the seq() function.

Example:

# Sequence from 1 to 10
seq1 <- 1:10
print(seq1)

# Sequence with a specific increment
seq2 <- seq(1, 10, by = 2)
print(seq2)

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:

# Generating 5 random numbers between 0 and 1
random_uniform <- runif(5)
print(random_uniform)

# Generating 5 random numbers with mean 0 and standard deviation 1
random_normal <- rnorm(5)
print(random_normal)

7. Handling Special Numbers

7.1 NA (Not Available)

NA represents missing values in R. Operations with NA typically return NA.

Example:

x <- NA
print(is.na(x))  # Output: TRUE

7.2 NaN (Not a Number)

NaN represents undefined mathematical operations, such as dividing zero by zero.

Example:

x <- 0 / 0
print(is.nan(x))  # Output: TRUE

7.3 Inf (Infinity)

Inf represents positive or negative infinity in R, typically resulting from division by zero.

Example:

x <- 1 / 0
print(x)  # Output: Inf

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:

# Coercing a numeric value to an integer
num <- 5.67
int <- as.integer(num)
print(int)  # Output: 5

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