R Number Data Types

In Previous topics we leared about different data types
Here we are going to about different types of data types which holds numbers
There are three types

Data Type

Description

Example

Numeric

Real or decimal numbers

num_var <- 3.14

Integer

Whole numbers

int_var <- 42L

Complex

Complex numbers

complex_var <- 2 + 3i


Example
# Numeric
num_var <- 3.14

# Integer
int_var <- 42L

# Complex
complex_var <- 2 + 3i

# Print variables
print(num_var)
print(int_var)
print(missing_var)

Type Conversion

It is a buit-in function method to convert a data type to another data type.

  1. as.integer() - Converts a numeric to an integer.
  2. as.numeric() - Converts an integer to a numeric.
  3. as.complex() - Converts numeric and integer to complex.
Example
# Numeric to Integer
num_var <- 42.75
int_var <- as.integer(num_var)

# Integer to Numeric
another_int_var <- 30L
numeric_from_int <- as.numeric(another_int_var)

# Numeric to Complex
complex_from_numeric <- as.complex(num_var)

# Integer to Complex
complex_from_int <- as.complex(another_int_var)

# Complex to Numeric
numeric_from_complex <- Re(complex_from_numeric)  # Taking the real part

# Print Results
print("Numeric to Integer:")
print(num_var)
print(int_var)

print("Integer to Numeric:")
print(another_int_var)
print(numeric_from_int)

print("Numeric to Complex:")
print(complex_from_numeric)

print("Integer to Complex:")
print(complex_from_int)

print("Complex to Numeric:")
print(numeric_from_complex)

Quick Recap - Topics Covered

R Lists

Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.


Example 1
Example 1 Example 2 Example 3 Example 4 Example 5


Quiz


FEEDBACK