0% found this document useful (0 votes)
6 views19 pages

BDA Section 4

Uploaded by

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

BDA Section 4

Uploaded by

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

ARAB ACADEMY FOR SCIENCE , TECHNOLOGY & MARITIME TRANSPORT

COLLEGE OF COMPUTING AND INFORMATION TECHNOLOGY

Big Data Analytics


Sec 03
Eng. Ahmed Mahmoud Nazif

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

#get list length


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

• Add element in specified position


#add “cherry” after “apple”
fruits <- list("banana", "apple", "mango")
fruits<- append(fruits, "cherry“, after=2) 8

fruits
Remove element from list

fruits <- list("banana", "apple", "mango“, “cherry”)


fruits["cherry"]<- NULL
Fruits
or by index
fruits <- list("banana", "apple", "mango“, “cherry”)
fruits[4]<- NULL
Fruits

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.

• The row names should be unique.

• The data stored in a data frame can be of numeric or character type.


• Each column should contain same number of data items.

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:

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),
row.names=c('row_1', 'row_2', 'row_3', 'row_4'))
print(Data)

# we can rename the columns of a DataFrame after its creation using the names() function:

names(Data) <- c('col_1', 'col_2', 'col_3', 'col_4')


print(Data)
13
Access Data Frame elements
• We can use single brackets [ ], double brackets [[ ]] or $ to access columns from a data frame.
• Example:
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)

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

# Add a new column


New_col_DF <- cbind(Data, Steps = c(1000, 6000, 5000,2000))
New_col_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)

# dim() function to find the number of rows and columns


dim(Data)

#Dataframe length
length(Data) 17
# Display first 6 rows of the data
head(Data)

# Display last 6 rows of the data


tail(Data)

# Specify number of rows for head or tail


head(Data,n=2)
tail(Data,n=2)

# Use the summary() function to summarize the data from a Data Frame.
summary(Data)

. 18
Thanks
19

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy