R Data Types

Data types serve as the fundamental building blocks that underpin how information is represented and manipulated within the language. There are several distinct data types in R, each tailored to handle specific types of information.

Numeric Data Type

This data type is used to store numerical values, including both integers and decimal numbers.

age <- 25 height <- 1.75

Character Data Type

Character data types are utilized to store text and alphanumeric values. These are enclosed within quotation marks, either single or double.

name <- "William" message <- "R, Programming!"

Integer Data Type

Integer data types represent whole numbers. They are similar to numeric data types, but they must be whole numbers.

age <- 25 number_of_students <- 100

Logical Data Type

Logical data types represent binary values denoting true or false conditions. They are indispensable for making decisions and performing logical operations.

is_student <- TRUE has_car <- FALSE

Complex Data Type

Complex data types are used to represent complex numbers with real and imaginary components.

complex_number <- 3 + 2i

Factor Data Type

Factors are used to categorically represent data with predefined levels or categories. They are particularly useful for statistical analyses.

gender <- factor(c("Male", "Female", "Male", "Female"))

Date and Time Data Types

R also supports date and time data types, which are essential for working with temporal data. These include Date and POSIXct types.

today <- as.Date("2023-08-16") timestamp <- as.POSIXct("2023-08-16 14:30:00", format = "%Y-%m-%d %H:%M:%S")

Raw Data Types

Raw data types represent raw data, such as binary data or hexadecimal data. They are not often used in R, but they can be useful for storing data that cannot be represented by other data types.

raw_data <- "This is some raw data"

In addition to these basic data types, R also has a number of composite data types, such as vectors, lists, matrices, and data frames. These composite data types are made up of multiple basic data types.

For example, a vector is a one-dimensional array of data. It can contain any of the basic data types, and the elements of a vector must be of the same data type.

vector_of_numbers <- c(1, 2, 3, 4, 5) vector_of_strings <- c("Hello", "world!") vector_of_logicals <- c(TRUE, FALSE, TRUE)

A list is a more general data type than a vector. It can contain any of the basic data types, and the elements of a list can be of different data types.

list_of_items <- list(name = "John Doe", age = 25, is_male = TRUE)

A matrix is a two-dimensional array of data. It can contain any of the basic data types, and the elements of a matrix must be of the same data type.

matrix_of_numbers <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2, ncol = 3)

A data frame is a special type of matrix that can contain multiple data types. It is the most common data type used in R for storing data.

data_frame <- data.frame(name = c("William", "Smith"), age = c(25, 26), is_male = c(TRUE, FALSE))

Conclusion

Data types in R define the nature of information representation. Numeric types handle numbers, character types store text, and logical types represent true or false conditions. Additional types include integers, complex numbers, factors for categorical data, and date/time types for temporal data manipulation.