In R we declare a variable with a data type to store the type of data.
R supports several fundamental data types.
Data Type
Description
Example
Numeric
Real or decimal numbers
num_var <- 3.14
Integer
Whole numbers
int_var <- 42L
Character
Text or strings
char_var <- "Hello, R!"
Logical
Boolean values (TRUE or FALSE)
logical_var <- TRUE
Factor
Categorical variables with levels
factor_var <- factor(c("Low", "Medium", "High"))
Vector
One-dimensional array
numeric_vector <- c(1, 2, 3, 4, 5)
Matrix
Two-dimensional array with rows and columns
matrix_var <- matrix(1:6, nrow = 2, ncol = 3)
Array
Multi-dimensional array
array_var <- array(1:24, dim = c(2, 3, 4))
Data Frame
Two-dimensional table with rows and columns
data <- data.frame(Name = c("John", "Jane", "Bob"), Age = c(25, 30, 22), Grade = c("A", "B", "C"))
List
Collection of elements with different data types
list_var <- list("apple", 3.14, TRUE)
NULL
Represents the absence of a value
null_var <- NULL
Missing Values (NA)
Represents missing or undefined values
missing_var <- NA
Complex
Complex numbers
complex_var <- 2 + 3i
Raw
Vector of bytes
raw_var <- as.raw(c(0x01, 0x02, 0x03))
Out of all Numeric, Integer, Character, Logical Data types are important because, remaining datatypes stores the values among the four data types only.
# Numeric
num_var <- 3.14
# Integer
int_var <- 42L
# Character
char_var <- "Hello, R!"
# Logical
logical_var <- TRUE
# Factor
factor_var <- factor(c("Low", "Medium", "High"))
# Vector
numeric_vector <- c(1, 2, 3, 4, 5)
char_vector <- c("apple", "banana", "orange")
# Matrix
matrix_var <- matrix(1:6, nrow = 2, ncol = 3)
# Array
array_var <- array(1:24, dim = c(2, 3, 4))
# Data Frame
data <- data.frame(
Name = c("John", "Jane", "Bob"),
Age = c(25, 30, 22),
Grade = c("A", "B", "C")
)
# List
list_var <- list("apple", 3.14, TRUE)
# NULL
null_var <- NULL
# Missing Values (NA)
missing_var <- NA
# Complex
complex_var <- 2 + 3i
# Raw
raw_var <- as.raw(c(0x01, 0x02, 0x03))
# Print variables
print(num_var)
print(int_var)
print(char_var)
print(logical_var)
print(factor_var)
print(numeric_vector)
print(char_vector)
print(matrix_var)
print(array_var)
print(data)
print(list_var)
print(null_var)
print(missing_var)
print(complex_var)
print(raw_var)
The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.