0% found this document useful (0 votes)
23 views18 pages

List and Data Frame

Uploaded by

Nikhil Kannale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views18 pages

List and Data Frame

Uploaded by

Nikhil Kannale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

R – Lists

A list in R programming is a generic object consisting of an ordered


collection of objects. Lists are one-dimensional or two-dimensional
etc data structures.
The list can be a list of vectors, a list of matrices, a list of characters, a
list of functions, and so on.
Creating a List
To create a List in R you need to use the function called “list()“.
Naming List Components
Naming list components make it easier to access them.
Example:
# Creating a named list
my_named_list <- list(name = "Sudheer", age = 25, city = "Delhi")

# Printing the named list


print(my_named_list)
output:
$name
[1] "Sudheer"

$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

# Creating a list by naming all its components


empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)

# Accessing components by names


cat("Accessing name components using $ command\n")
print(empList$Names)
Output:
$ID
[1] 1 2 3 4

$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"

$`Total Staff`
[1] 4

Accessing name components using $ command


[1] "Debi" "Sandeep" "Subham" "Shiba"
2. Access components by indices:
We can also access the components of the R list using indices.
To access the top-level components of a R list we have to use a double
slicing operator “[[ ]]” which is two square brackets and if we want to
access the lower or inner-level components of a R list we have to use
another square bracket “[ ]” along with the double slicing operator
“[[ ]]“.
Example:
# R program to access
# components of a list

# Creating a list by naming all its components


empId = c(1, 2, 3, 4)
empName = c("Debi", "Sandeep", "Subham", "Shiba")
numberOfEmp = 4
empList = list(
"ID" = empId,
"Names" = empName,
"Total Staff" = numberOfEmp
)
print(empList)

# Accessing a top level components by indices


cat("Accessing name components using indices\n")
print(empList[[2]])

# Accessing a inner level components by indices


cat("Accessing Sandeep from name using indices\n")
print(empList[[2]][2])

# Accessing another inner level components by indices


cat("Accessing 4 from ID using indices\n")
print(empList[[1]][4])
output:
$ID
[1] 1 2 3 4

$Names
[1] "Debi" "Sandeep" "Subham" "Shiba"
$`Total Staff`
[1] 4

Accessing name components using indices


[1] "Debi" "Sandeep" "Subham" "Shiba"
Accessing Sandeep from na...
Converting List to Vector
Here we are going to convert the R list to vector, for this we will
create a list first and then unlist the list into the vector.

# Create lists.
lst <- list(1:5)
print(lst)

# Convert the lists to vectors.


vec <- unlist(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")

# Convert list to matrix


mat <- matrix(unlist(lst1), nrow = 2, byrow = TRUE)

# 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(

# Taking sequence of elements


c(1, 2, 3, 4, 5, 6, 7, 8, 9),

# No of rows
nrow = 3,

# No of columns
ncol = 3,

# By default matrices are in column-wise order


# So this parameter decides how to arrange the matrix
byrow = TRUE
)

# Naming rows
rownames(A) = c("a", "b", "c")

# Naming columns
colnames(A) = c("c", "d", "e")

cat("The 3x3 matrix:\n")


print(A)
Output
The 3x3 matrix:
cde
a123
b456
c789

1. Matrix where all rows and columns are filled by a single


constant ‘k’:
To create such a R matrix the syntax is given below:
Syntax: matrix(k, m, n)
Parameters:
k: the constant
m: no of rows
n: no of columns
Example:
R
# R program to illustrate
# special matrices

# Matrix having 3 rows and 3 columns


# filled by a single constant 5
print(matrix(5, 3, 3))

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

# Identity matrix having


# 3 rows and 3 columns
print(diag(1, 3, 3))
Output
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1

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

# Create a 3x3 matrix


A = matrix(
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
nrow = 3,
ncol = 3,
byrow = TRUE
)
cat("The 3x3 matrix:\n")
print(A)

cat("Dimension of the matrix:\n")


print(dim(A))

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 the data frame


print(my_data_frame)
Output:
graphql
Copy code
name age scores is_student
1 John 25 90 TRUE
2 Alice 30 85 FALSE
3 Bob 22 88 TRUE
Example 2: Creating a Data Frame from Existing
Vectors
r
Copy code
# Vectors
names <- c("John", "Alice", "Bob")
ages <- c(25, 30, 22)
scores <- c(90, 85, 88)
students <- c(TRUE, FALSE, TRUE)

# Combine vectors into a data frame


my_data_frame <- data.frame(name = names, age =
ages, scores = scores, is_student = students)

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

You might also like