📚 R Programming Tutorial

R Programming

Learn R programming concepts with simple explanations, examples, techniques and practical notes.

← Back to R Programming

Datatypes in R

In general, Data types specify what type of data will be stored in variables. In other words, the variables can hold values of different data types.

In R, no need to specify the type of variable because the variable automatically changes its datatype based on assigned value.

R provides the class() function which enables us to check the data type of the variable.

R has several basic data types, which include:

  • numeric
  • integer
  • complex
  • character (a.k.a. string)
  • logical (a.k.a. boolean)

Numeric Data Type

The numeric data type in R is used to represent all real numbers, whether they have decimal points or not. Like 12, 15.6, 456, -78, -56.3.

Example:

Filename: numeric_d.R

# without decimals
age <- 23
print(age)

# with decimals
weight <- 48.5
print(weight)

# print data type of variables
print(class(age))
print(class(weight))

Output:

Rscript numeric_d.R
[1] 23
[1] 48.5
[1] "numeric"
[1] "numeric"

Integer Data Type

The integer data type is used to represent real values without decimal points. We use the suffix L to specify integer type data. For example, 45L, 123L, 78L, -45L.

Example:

Filename: integer_d.R

# without decimals
age <- 23L
print(age)

# print data type of variables
print(class(age))

Output:

Rscript integer_d.R
[1] 23
[1] "integer"

Complex Data Type

The complex data type is used to specify purely imaginary values in R. We use the suffix i to specify the imaginary part. For example, 3+2i, -2+5i.

Example:

Filename: complex_d.R

val <- 5 + 6i
print(val)

# print data type of variables
print(class(val))

Output:

Rscript complex_d.R
[1] 5+6i
[1] "complex"

Character Data Type

The character data type is used to specify character or string values in a variable.

In programming, a string is a set of characters. For example, 'A' is a single character and "Apple" is a string.

You can use single quotes (' ') or double quotes (" ") to represent strings. In general, we use:

  • Single quotes (' ') for character values
  • Double quotes (" ") for string values

Example:

Filename: character_d.R

# create a string variable
fname <- "Apple"
print(class(fname))

# create a character variable
ch <- 'A'
print(class(ch))

Output:

Rscript character_d.R
[1] "character"
[1] "character"

Logical Data Type

The logical data type in R is also known as boolean data type. It can only have two values: TRUE and FALSE.

Example:

Filename: logical_d.R

b_val1 <- TRUE
print(b_val1)
print(class(b_val1))

b_val2 <- FALSE
print(b_val2)
print(class(b_val2))

Output:

Rscript logical_d.R
[1] TRUE
[1] "logical"
[1] FALSE
[1] "logical"

Special Values in R

R has some special values:

  • NA : Missing data.
  • NULL : Represents an empty or undefined value.
  • NaN : Not a number (e.g., 0/0).
  • Inf and -Inf : Positive and negative infinity (e.g., division by zero).