List and Data Frame
List and Data Frame
$age
[1] 25
$city
[1] "Delhi"
Accessing R List Components
We can access components of an R list in two ways.
1. Access components by names:
All the components of a list can be named and we can use those
names to access the components of the R list using the dollar
command.
Example:
# R program to access
# components of a list
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4
# Create lists.
lst <- list(1:5)
print(lst)
print(vec)
Output
[[1]]
[1] 1 2 3 4 5
[1] 1 2 3 4 5
R List to matrix
We will create matrices using matrix() function in R
programming. Another function that will be used is unlist()
function to convert the lists into a vector.
# Defining list
lst1 <- list(list(1, 2, 3),
list(4, 5, 6))
# Print list
cat("The list is:\n")
print(lst1)
cat("Class:", class(lst1), "\n")
# Print matrix
cat("\nAfter conversion to matrix:\n")
print(mat)
cat("Class:", class(mat), "\n")
Creating a Matrix in R
To create a matrix in R you need to use the function
called matrix().
The arguments to this matrix() are the set of elements in the
vector. You have to pass how many numbers of rows and how
many numbers of columns you want to have in your matrix.
Parameters:
data – values you want to enter
nrow – no. of rows
ncol – no. of columns
byrow – logical clue, if ‘true’ value will be assigned by rows
dimnames – names of rows and columns
Example:
# R program to create a matrix
A = matrix(
# No of rows
nrow = 3,
# No of columns
ncol = 3,
# Naming rows
rownames(A) = c("a", "b", "c")
# Naming columns
colnames(A) = c("c", "d", "e")
Output
[,1] [,2] [,3]
[1,] 5 5 5
[2,] 5 5 5
[3,] 5 5 5
2. Diagonal matrix:
A diagonal matrix is a matrix in which the entries outside the
main diagonal are all zero. To create such a R matrix the syntax is
given below:
Syntax: diag(k, m, n)
Parameters:
k: the constants/array
m: no of rows
n: no of columns
Example:
R
# R program to illustrate
# special matrices
# Diagonal matrix having 3 rows and 3 columns
# filled by array of elements (5, 3, 3)
print(diag(c(5, 3, 3), 3, 3))
Output
[,1] [,2] [,3]
[1,] 5 0 0
[2,] 0 3 0
[3,] 0 0 3
3. Identity matrix:
An identity matrix in which all the elements of the principal
diagonal are ones and all other elements are zeros. To create such
a R matrix the syntax is given below:
Syntax: diag(k, m, n)
Parameters:
k: 1
m: no of rows
n: no of columns
Example:
R
# R program to illustrate
# special matrices
4. Matrix Metrics
Matrix metrics tell you about the Matrix you created. You might
want to know the number of rows, number of columns,
dimensions of a Matrix.
Below Example will help you in answering following questions:
How can you know the dimension of the matrix?
How can you know how many rows are there in the matrix?
How many columns are in the matrix?
How many elements are there in the matrix?
Example:
# R program to illustrate
# matrix metrics
cat("Number of rows:\n")
print(nrow(A))
cat("Number of columns:\n")
print(ncol(A))
cat("Number of elements:\n")
print(length(A))
# OR
print(prod(dim(A)))
Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Dimension of the matrix:
[1] 3 3
Number of rows:
[1] 3
Number of columns:
[1] 3
Number of elements:
[1] ...
Data Frame In R
A data frame in R is a two-dimensional structure,
similar to a table or spreadsheet, where each column
can contain different types of data (numeric, character,
factor, etc.), but all columns must have the same
number of rows.
Key Characteristics of a Data Frame:
Rows represent individual records or observations.
Columns represent variables, each of which can
have a different data type.
Mixed Data Types: Columns can hold data of
varying types, such as characters, numerics, and
factors.
Homogeneous Lengths: All columns must have
the same number of rows.
Creating a Data Frame
Example 1: Creating a Simple Data Frame
r
Copy code
# Create a data frame
my_data_frame <- data.frame(
name = c("John", "Alice", "Bob"),
age = c(25, 30, 22),
scores = c(90, 85, 88),
is_student = c(TRUE, FALSE, TRUE)
)
print(my_data_frame)
Accessing Data in a Data Frame
Accessing Columns
Using the $ operator to access a column by name:
r
Copy code
print(my_data_frame$name) # Access 'name' column
Using square brackets to access by index:
r
Copy code
print(my_data_frame[, 1]) # Access the first column
Accessing Rows
Access the first row:
r
Copy code
print(my_data_frame[1, ])
Accessing Specific Elements
Access the element in the first row and second
column:
r
Copy code
print(my_data_frame[1, 2])
Adding New Columns
You can add a new column to a data frame like this:
r
Copy code
my_data_frame$city <- c("New York", "Los Angeles",
"Chicago")
print(my_data_frame)
Modifying Data
You can modify specific values:
r
Copy code
# Change the score of the second person (Alice) to 95
my_data_frame$scores[2] <- 95
Summary Statistics for Data Frames
You can generate summaries and statistics for a data
frame:
r
Copy code
# Get a summary of the data frame
summary(my_data_frame)
Useful Functions for Data Frames
nrow(df): Get the number of rows.
ncol(df): Get the number of columns.
colnames(df): Get the names of the columns.
head(df): Display the first few rows.
tail(df): Display the last few rows.
Example: Loading Data from External Files
You can load data into a data frame from a CSV file:
r
Copy code
my_data_frame <- read.csv("data.csv")
Example: Subsetting a Data Frame
You can subset a data frame by rows or columns:
r
Copy code
# Select rows where age is greater than 25
subset_df <- subset(my_data_frame, age > 25)
Combining Data Frames
You can combine data frames using functions like
rbind() (for rows) and cbind() (for columns).
This gives a foundational understanding of data frames
in R. Data frames are a central tool for managing
datasets in R, particularly for statistical analysis and
data manipulation.
4o