top of page

R Numbers

  • Numeric: It represents both whole and floating-point numbers. For example, 123, 32.43, etc.

  • Integer: It represents only whole numbers and is denoted by L. For example, 23L, 39L, etc.

  • Complex: It represents complex numbers with imaginary parts. The imaginary parts are denoted by i. For example, 2 + 3i, 5i, etc.


Numeric Data Type


Numeric data type is the most frequently used data type in R. It is the default data type whenever you declare a variable with numbers.

You can store any type of number (with or without decimal) in a variable with numeric data type. For example,


# decimal variable
my_decimal <- 123.45
print(class(my_decimal))

# variable without decimal
my_number <- 34
print(class(my_number))

Output

[1] "numeric"
[1] "numeric"

Here, both the my_decimal and my_number variables are of numeric type.


Integer Data Type


Integers are a type of numeric data that can take values without decimal. It's mostly used when you are sure that the variable can not have any decimal values in the future.


In order to create an integer variable, you must use the suffix L at the end of the value. For example,

my_integer <- 123L

# print the value of my_integer
print(my_integer) 

# print the data type of my_integer
print(class(my_integer))

Output


[1] 123
[1] "integer"

Here, the variable my_integer contains the value 123L. The suffix L at the end of the value indicates that my_integer is of integer type.


Complex Data Type


In R, variables with complex data types contain values with an imaginary part. This can be indicated by using the i as a suffix. For example,


# variable with only imaginary part
z1 <- 5i
print(z1)
print(class(z1))

# variable with both real and imaginary parts
z2 <- 3 + 3i
print(z2)
print(class(z2))

Output

[1] 0+5i
[1] "complex"
[1] 3+3i
[1] "complex"

Here, the variables z1 and z2 have been declared as complex data types with an imaginary part denoted by the suffix i.

Related Posts

See All

R Data Frame: A Comprehensive Guide

Welcome to Codes With Pankaj! In this tutorial, we’ll dive deep into one of the most versatile and commonly used data structures in R -...

Kommentare


bottom of page