Numeric Data Type in R Language

Decimal values are referred to as numeric data types in R, the default working out data type for numbers in R Language. Assigning a decimal value to a variable $x$ creates a variable that has a numeric data type. For example

x <- 6.2
print(x)

Since numeric data types consist of numbers, one can perform different mathematical operations such as addition, subtraction, multiplication, division, etc.

In R, the class of numeric variables is numeric. One can check the class of a numeric object ($x$) by using class() function.

class(x)
Numeric Data Type in R

In R Language, the as.numeric() function is used to convert a vector of character values to a numeric value. Note that by default, R converts character vectors to factors.

One can confirm the data type of an object by using a function is.numeric(). For example,

is.numeric(x)

If is.numeric(x) results in an output of TRUE then it means that the data type of the variable/object $x$ is numeric. Let’s assign a whole number to a variable $y$ and then check the class of object $y$:

y <- 2
class(y)
[1] "numeric"

It means that the default data type for numbers is the numeric type in R Language. One can also use typeof() function to confirm the data type of a variable.

One can also create a variable (called a numeric vector) by using the numeric() function. It will create a vector of zeros. For example,

z <- numeric(5)
print(z)

[1] 0 0 0 0 0

class(z)

[1] "numeric"


Other methods also exist for the creation of numeric vectors. Note that the numeric data type is different from the integer.

MCQs Data Basic Statistics Quiz

In Summary, the numeric data type is a fundamental data structure for numerical computations in R. Understanding its properties and when to potentially use the integer data type is essential for effective data analysis in R.

Leave a Reply

Discover more from R Frequently Asked Questions

Subscribe now to keep reading and get access to the full archive.

Continue reading