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

A Comprehensive Guide to R Data Types

Introduction In the realm of data analysis and statistical computing, the R programming language stands as a powerful tool. Understanding data types in R is fundamental to unleashing its full potentia

R - CSV Files

Getting and Setting the Working Directory You can check which directory the R workspace is pointing to using the getwd() function. You can also set a new working directory using setwd()function. # Get

Comments


bottom of page