R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
R, being a language primarily designed for statistics and data analysis, offers a wide variety of data types to handle different forms of data. Understanding these data types is fundamental to working with R efficiently. In this tutorial, we'll cover the main data types you'll encounter in R.
This is the default computational data type in R. If you input a number into R, it will assume it's numeric unless you specify otherwise.
x <- 23.5 class(x) # Output: "numeric"
To specify an integer, use the L
suffix.
y <- 23L class(y) # Output: "integer"
R can handle complex numbers with an imaginary component.
z <- 3 + 4i class(z) # Output: "complex"
Boolean values (TRUE
/FALSE
).
a <- TRUE b <- (4 > 5) # This will be FALSE class(a) # Output: "logical"
Text/string values.
name <- "Alice" class(name) # Output: "character"
A one-dimensional array that can hold numeric, integer, complex, logical, or character values. Elements in a vector must be of the same type.
vec <- c(1, 2, 3, 4, 5) class(vec) # Output: "numeric"
Two-dimensional arrays where every element is of the same type.
mat <- matrix(1:6, nrow=2, ncol=3) class(mat) # Output: "matrix"
Like matrices but can have more than two dimensions.
arr <- array(1:12, dim=c(2,3,2)) class(arr) # Output: "array"
Vectors used to represent categorical data.
gender <- factor(c("Male", "Female", "Male")) class(gender) # Output: "factor"
Tabular data structure with columns that can be of different types.
df <- data.frame(Name=c("Alice", "Bob"), Age=c(25, 30)) class(df) # Output: "data.frame"
Collections of objects, where each object can be of a different type.
lst <- list(Name="Alice", Age=25, Scores=c(90, 85, 92)) class(lst) # Output: "list"
You can use is.
functions to check data types, e.g., is.numeric()
, is.character()
, is.matrix()
, etc.
You can convert one data type to another using as.
functions:
num <- 5 as.character(num)
However, be cautious when coercing, as it can lead to data loss or unexpected results.
R provides a rich set of data types and structures to handle a wide range of data analysis tasks. Familiarizing yourself with these data types will help you use R more efficiently and avoid common pitfalls. As you progress, you'll learn about even more advanced data structures and types provided by various R packages.
Numeric and integer data types in R:
# Numeric data type num_var <- 3.14 # Integer data type int_var <- 42L
Character and string data types in R programming:
# Character data type char_var <- "Hello, World!" # String data type str_var <- 'R Programming'
Logical and boolean data types in R:
# Logical data type logical_var <- TRUE # Boolean data type bool_var <- FALSE
Handling date and time data types in R:
# Date data type date_var <- as.Date("2022-01-01") # POSIXct for date and time datetime_var <- as.POSIXct("2022-01-01 12:30:00")
Factor data type in R explained:
# Creating a factor gender <- c("Male", "Female", "Male", "Female") factor_gender <- factor(gender) # Viewing levels of a factor levels(factor_gender)
Working with lists and vectors in R:
# Creating a vector my_vector <- c(1, 2, 3, 4, 5) # Creating a list my_list <- list("John", 25, TRUE, c(1, 2, 3))
Conversion between data types in R:
# Numeric to character conversion num_to_char <- as.character(num_var) # Character to numeric conversion char_to_num <- as.numeric(char_var) # Integer to numeric conversion int_to_num <- as.numeric(int_var) # Date to character conversion date_to_char <- as.character(date_var) # Character to date conversion char_to_date <- as.Date(date_to_char)