Program :
# Different Types of Data Structures in R
# 1. Vectors
vectors <- c(10, 20, 30, 40, 50)
cat("-------Vector-------:\n")
print(vectors)
# 2. Lists
lists <- list(name = "Alice", age = 30, scores = c(85, 90, 95))
cat("\n-------List-------:\n")
print(lists)
# 3. Matrices
matrix <- matrix(1:12, nrow = 3, ncol = 4)
cat("\n-------Matrix-------:\n")
print(matrix)
# 4. Arrays
arrays <- array(1:24, dim = c(3, 4, 2))
cat("\n-------Arrays------:\n")
print(arrays)
# 5. Data Frames
df <- data.frame(
ID = 1:3,
Name = c("Alice", "Bob", "Charlie"),
Score = c(85, 92, 88)
)
cat("\n------Data Frame-------:\n")
print(df)
# 6. Factors
fct <- factor(c("High", "Medium", "Low", "Medium", "High"))
cat("\n-------Factor-------:\n")
print(fct)
# 1. Vectors
vectors <- c(10, 20, 30, 40, 50)
cat("-------Vector-------:\n")
print(vectors)
# 2. Lists
lists <- list(name = "Alice", age = 30, scores = c(85, 90, 95))
cat("\n-------List-------:\n")
print(lists)
# 3. Matrices
matrix <- matrix(1:12, nrow = 3, ncol = 4)
cat("\n-------Matrix-------:\n")
print(matrix)
# 4. Arrays
arrays <- array(1:24, dim = c(3, 4, 2))
cat("\n-------Arrays------:\n")
print(arrays)
# 5. Data Frames
df <- data.frame(
ID = 1:3,
Name = c("Alice", "Bob", "Charlie"),
Score = c(85, 92, 88)
)
cat("\n------Data Frame-------:\n")
print(df)
# 6. Factors
fct <- factor(c("High", "Medium", "Low", "Medium", "High"))
cat("\n-------Factor-------:\n")
print(fct)
Output :
-------Vector-------:
[1] 10 20 30 40 50
[1] 10 20 30 40 50
-------List-------:
$name
[1] "Alice"
$age
[1] 30
$scores
[1] 85 90 95
-------Matrix-------:
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
-------Arrays------:
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
------Data Frame-------:
ID Name Score
1 1 Alice 85
2 2 Bob 92
3 3 Charlie 88
------Factor------ :
[1] High Medium Low Medium High
Levels: High Low Medium
=== Session Ended. Please Run the code again ===
Levels: High Low Medium
=== Session Ended. Please Run the code again ===
Concepts :
Cat and Print :
The
cat
and print
functions in R are both used to display output, but they serve different purposes and exhibit different behaviors. The cat
function is used to concatenate and output text strings directly, without any additional formatting, and it does not return a value (it returns NULL
invisibly). It is useful for creating custom formatted text outputs, such as writing to files or generating specific console messages. On the other hand, the print
function is designed to display objects in a human-readable format, automatically adding default formatting (such as quotes around strings or structured display for complex objects). print
returns the object it displays, which makes it useful for quickly checking the contents of variables during development and debugging. Overall, cat
is preferred for precise text output control, while print
is suitable for general-purpose display of R objects.Vector :
Vector is a fundamental data structure that represents an ordered collection of elements of the same type, such as numeric, character, or logical. Vectors are created using the
c()
function, which combines individual values into a single vector. They are versatile and support a variety of operations, including element-wise arithmetic, subsetting using square brackets, and applying functions like sum()
, mean()
, and length()
to aggregate or summarize the data. Vectors are essential for data manipulation and analysis in R, serving as the building blocks for more complex data structures like matrices and data frames. Their homogeneous nature ensures efficient storage and computation, making them a powerful tool in the R programming environment.List :
List is a versatile data structure that can contain elements of different types and structures, including numbers, strings, vectors, and even other lists. This heterogeneity makes lists highly flexible, allowing them to store complex and varied data within a single object. Lists are created using the
list()
function, where each element can be named for easier reference. Elements within a list can be accessed and modified using double square brackets [[ ]]
or the $
operator for named elements. Lists are particularly useful for organizing and managing related data items that do not conform to the strict type uniformity required by vectors and matrices, making them indispensable for handling complex data in R programming.Matrix :
Matrix is a two-dimensional data structure that holds elements of the same type, organized into rows and columns. Matrices are created using the
matrix()
function, where you specify the data, the number of rows (nrow
), and the number of columns (ncol
). They allow for mathematical and statistical operations to be performed efficiently, as these operations are often optimized for matrix calculations. Elements in a matrix can be accessed using square brackets [ ]
with row and column indices. Matrices are particularly useful for representing and manipulating data in fields such as linear algebra, statistics, and data analysis, where structured, two-dimensional data is common. Their ability to handle homogeneous data makes them a powerful tool for numerical computations in R.Array :
An array is a multi-dimensional data structure that generalizes matrices to more than two dimensions, allowing for the storage of data in an ordered, multi-dimensional format. Arrays are created using the
array()
function, where you provide the data and specify the dimensions through the dim
parameter. Each dimension in an array represents a different level of indexing, making arrays suitable for representing complex data such as time-series data across multiple categories or multi-factor experimental data. All elements within an array must be of the same type, maintaining type consistency across dimensions. Arrays can be accessed and manipulated using multi-dimensional indexing with square brackets [ ]
. Their ability to handle structured, multi-dimensional data efficiently makes them a powerful tool for advanced data analysis and modeling in R.Data Frame:
Data frame is a widely used data structure that organizes data into a table format, where each column can contain elements of different types (e.g., numeric, character, factor), but each column must have elements of the same type. Created using the
data.frame()
function, data frames are particularly useful for statistical modeling and data analysis, resembling a spreadsheet or a SQL table. They allow for easy manipulation, subsetting, and transformation of data, with columns accessed using the $
operator or square brackets. Data frames support a variety of operations, such as filtering rows, selecting columns, and applying functions to data, making them essential for managing and analyzing structured datasets in R.Factors :
Factors are a data structure used to handle categorical data, which can be either ordered or unordered. Created using the
factor()
function, factors store both the unique values (levels) and the actual data as integers that point to these levels. This structure is particularly useful for statistical modeling and data analysis, as it allows for efficient storage and manipulation of categorical data, such as gender, survey responses, or experimental treatments. Factors support operations like subsetting and comparison and can be ordered to represent ordinal data. They ensure consistency in the data by restricting the input to predefined levels, making them a powerful tool for managing categorical variables in R.
No comments:
Post a Comment