Lab 2 - Lists, Vectors, Dataframes

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Lists, Vectors, Data

frames in R
Lists in R
Lists are the R objects which contain elements of different types
like − numbers, strings, vectors and another list inside it. A list can
also contain a matrix or a function as its elements. List is created
using list() function

# List of strings
thislist <- list("apple", "banana", "cherry")
thislist

#find the length of the list


thislist <- list("apple", "banana", "cherry")
length(thislist)
Lists in R
# Creating lists.

list1 <- list(10:20)


print(list1)
list2 <-list(5:14)
print(list2)
Range of Indexes

thislist <-
list("apple", "banana", "cherry", "orange",
"kiwi", "melon", "mango")
(thislist)[2:5]
R Vectors
A vector is simply a list of items that are of the
same type.

# Vector of numerical values

numbers <- c(1, 2, 3)


numbers

# Vector with numerical values in a sequence

numbers <- 1:10


numbers
Defining Vectors
fruits <-
c("banana", "apple", "orange", "mango", "lemon"
)
numbers <- c(13, 3, 5, 7, 20, 2)

sort(fruits) # Sort a string


sort(numbers) # Sort numbers
Accessing Vector Elements
 Use the [] operator to select elements

 To select specific elements:


• Use index or vector of indexes to identify them

 To exclude specific elements:


• Negate index or vector of indexes

 Alternative:
• Use vector of T and F values to select subset of elements
Data frames in R
• A data frame is a table or a two-dimensional array-
like structure in which each column contains values
of one variable and each row contains one set of
values from each column.
Following are the characteristics of a data frame.
• The column names should be non-empty.
• The row names should be unique.
• The data stored in a data frame can be of numeric,
factor or character type.
• Each column should contain same number of data
items
Data frame
# Create a data frame

Data_Frame <- data.frame (


Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
Data_Frame
Adding new row in Data Frames
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)

# Add a new row


New_row_DF <- rbind(Data_Frame,
c("Strength", 110, 110))

# Print the new row


New_row_DF
Lists
 Collections of related variables

 Created with list function


• point <- list(x = 1, y = 1)

Eg:
fruits <- list("apple", "banana", "cherry")

for (x in fruits) {
print(x)
}
Control Structures
 Control statements
• if … else …

• for loops
• repeat loops
• while loops
Largest of two numbers
using simple if
a <- 33
b <- 200

if (b > a)
{
print("b is greater than a")
}
Largest of two numbers using
if … else
a <- 33
b <- 33

if (b > a)
{
print("b is greater than a")
} else
{
print ("a and b are equal")
}
For Loop

Syntax:
for (name in expr_1) expr_2

Example:

for (x in 1:10)
{
print(x)
}
while

while (expr_1) expr_2

 While expr_1 is false, repeatedly evaluate


expr_2

 break and next statements can be used


within the loop

You might also like