BDA Section 4
BDA Section 4
1
R data structure
2
Lists
3
Lists
• A list in R can contain many different data types or structures inside it, like:
numbers, character, or even vector and other lists.
• To create a list, use the list() function.
• Example:
my_list <- list("apple" , 20 , TRUE , c(1,2,3) , 10.5)
my_list
4
Naming a list
• You can name list elements by 2 ways: while defining the list or
using names() function.
lst <- list(name= "ahmed", age= 20 , tall=170)
lst
#using names()
lst <- list("ahmed", 20 , 170)
names(lst)<-c("name" , "age" , "tall")
lst
5
Check type
lst <- list(name= "ahmed", age= 20 , tall=170)
lst
# to display structure
str(lst)
6
Access & change list elements
• Like vectors you can access single element or multiple elements using element index
or refer to the element by its name.
#access element
lst <- list(name= "ahmed", age= 20 , tall=170)
lst [1]
lst["name"]
#Change element
lst [1] <- "mohamed“
or
lst["name"]<- "mohamed"
7
Add elements to list
You can add element by assign to index or using the append() function
Examples:
#Add “cherry" to the list:
fruits <- list("banana", "apple", "mango")
fruits[4] <- "cherry“
#Using append()
fruits <- list("banana", "apple", "mango")
fruits<- append(fruits, "cherry")
fruits
fruits
Remove element from list
9
Data Frames
10
Data Frames
• 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.
11
• To create a DataFrame in R from one or more vectors of the same length, we use the data.frame()
function.
# basic syntax is as follows
df <- data.frame(vector_1, vector_2)
• Example:
name <- c('Mohamed', 'Ahmed', 'Nour', 'Menna')
age <- c(20,30,40,50)
country <- c('Australia', 'Italy', 'Egypt', 'China')
salary <- c(2000, 1800, 1700, 1000)
Data <- data.frame(name, age, country, salary)
print(Data)
# we can provide all the vectors directly in the following function:
Data <- data.frame(name = c('Mohamed', 'Ahmed', 'Nour', 'Menna'),
age = c(20,30,40,50),
country = c('Australia', 'Italy', 'Egypt', 'China'),
salary = c(2000, 1800, 1700, 1000))
print(Data) 12
Define the columnand rownames
# we use an optional parameter row.names, as follows:
# we can rename the columns of a DataFrame after its creation using the names() function:
Data[3]
Data[["country"]]
Data$country
14
Add Rows and Columns
• Use the cbind() function to add additional columns and rbind() to add additional rows in a data frame.
Note: The cells in the new column or row must be of the same length as the existing data frame.
#Add a new row
New_row_DF <- rbind(Data, c("Mona", 110,"Italy" ,1100))
New_row_DF
..
15
Remove Rows and Columns
• using the c() function to remove rows and columns in a Data frame.
Data <- data.frame(name = c('Mohamed', 'Ahmed', 'Nour', 'Menna'),
age = c(20,30,40,50),
country = c('Australia', 'Italy', 'Egypt', 'China'),
salary = c(2000, 1800, 1700, 1000))
print(Data)
# Remove the first row and column
Data_New <- Data[-c(1), -c(1)]
Data_New
16
# Get column names
names(Data)
# to display structure
str(Data)
#Dataframe length
length(Data) 17
# Display first 6 rows of the data
head(Data)
# Use the summary() function to summarize the data from a Data Frame.
summary(Data)
. 18
Thanks
19