R Data Types


R Data Types

In this tutorial, we will learn about data types in R. We will cover the basics of different data types available in R, including numeric, integer, character, logical, and complex types, as well as how to work with them.


Understanding Data Types in R

R supports several basic data types that are used to store different kinds of values. Knowing these data types is fundamental to working with R.


Numeric Data Type

The numeric data type in R is used to store both integer and floating-point numbers. By default, numbers in R are stored as double precision floating-point numbers.

num <- 42.5
print(num)
class(num)

Integer Data Type

The integer data type is used to store integer values. To explicitly create an integer, append an L to the number.

int <- 42L
print(int)
class(int)

Character Data Type

The character data type is used to store text or string values. Character strings are enclosed in either single or double quotes.

char <- "Hello, R!"
print(char)
class(char)

Logical Data Type

The logical data type is used to store boolean values: TRUE or FALSE.

flag <- TRUE
print(flag)
class(flag)

Complex Data Type

The complex data type is used to store complex numbers.

comp <- 3 + 4i
print(comp)
class(comp)

Raw Data Type

The raw data type is used to store raw byte data. It is commonly used for storing binary data or data that does not fit into other data types.

raw_data <- as.raw(c(0x41, 0x42, 0x43))
print(raw_data)
class(raw_data)


Working with Numeric Data Type

In R, working with numeric data types involves performing arithmetic operations, statistical calculations, and using various built-in functions to manipulate and analyze numeric values.

For example,

  1. Create a numeric variable named num and assign it a floating-point value of 42.5.
  2. Print the value of num to the console.
  3. Use the class function to determine the data type of num and print the result.

R Program

num <- 42.5
print(num)
print(class(num))

Output

[1] 42.5
[1] "numeric"


Working with Integer Data Type

In R, working with integer data types involves handling whole numbers, performing arithmetic operations, and using specific functions to ensure data is treated as integers for precise calculations.

For example,

  1. Create an integer variable named int and assign it an integer value of 42 using the L suffix.
  2. Print the value of int to the console.
  3. Use the class function to determine the data type of int and print the result.

R Program

int <- 42L
print(int)
print(class(int))

Output

[1] 42
[1] "integer"


Working with Character Data Type

In R, working with character data types involves manipulating strings, performing text operations, and utilizing functions like paste, substr, and gsub for various text processing tasks.

For example,

  1. Create a character variable named char and assign it a string value of "Hello, R!".
  2. Print the value of char to the console.
  3. Use the class function to determine the data type of char and print the result.

R Program

char <- "Hello, R!"
print(char)
print(class(char))

Output

[1] "Hello, R!"
[1] "character"


Working with Logical Data Type

In R, working with logical data types involves handling TRUE and FALSE values, performing logical operations, and using them in control structures like if statements and loops.

For example,

  1. Create a logical variable named flag and assign it a value of TRUE.
  2. Print the value of flag to the console.
  3. Use the class function to determine the data type of flag and print the result.

R Program

flag <- TRUE
print(flag)
print(class(flag))

Output

[1] TRUE
[1] "logical"


Working with Complex Data Type

In R, working with complex data types involves handling numbers with real and imaginary parts, performing arithmetic operations, and using functions like Re, Im, and Mod to manipulate them.

For example,

  1. Create a complex variable named comp and assign it a value of 3 + 4i.
  2. Print the value of comp to the console.
  3. Use the class function to determine the data type of comp and print the result.

R Program

comp <- 3 + 4i
print(comp)
print(class(comp))

Output

[1] 3+4i
[1] "complex"


Working with Raw Data

In R, working with raw data involves dealing with binary data in its raw form, often used for low-level manipulation, such as reading/writing binary files or encoding/decoding data.

For example,

  1. We start by creating a raw vector using the as.raw() function, passing hexadecimal values as arguments.
  2. We print the raw vector to see its contents.
  3. We use the class() function to check the data type of the raw vector.

R Program

raw_data <- as.raw(c(0x41, 0x42, 0x43))
print(raw_data)
class(raw_data)

Output

[1] 41 42 43
[1] "raw"