0% found this document useful (0 votes)
17 views6 pages

5 Data Frame Operations

This document outlines various operations that can be performed on data frames in R, including definition, indexing, concatenation, merging, and filtering. It explains the structure of a data frame and provides examples of how to manipulate data frames using R code. Additionally, it includes an exercise to filter the built-in 'mtcars' data frame based on specific conditions.

Uploaded by

KwongYew Ngu
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)
17 views6 pages

5 Data Frame Operations

This document outlines various operations that can be performed on data frames in R, including definition, indexing, concatenation, merging, and filtering. It explains the structure of a data frame and provides examples of how to manipulate data frames using R code. Additionally, it includes an exercise to filter the built-in 'mtcars' data frame based on specific conditions.

Uploaded by

KwongYew Ngu
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/ 6

Data frame operations

At the end of this session, you will be able to perform


the following operations on data frames:
● Definition
● Indexing and reassigning
● Concatenation
● Merging
● Filtering

Elements of a data frame


● A data frame is like a matrix, in which different
columns contain different mode of data (numeric
vs string)
● What the computer sees, however, is actually a
list of vectors, with each vector having the same
length
Elements of a data frame
Motor
● Trend Car Road Tests
Attribute
Header

Data row

Cell

Elements of a data frame


Try:
> head(mtcars)
> mtcars$mpg
> mtcars[1, ]
> mtcars[1, 2]
> mtcars["Mazda RX4", "cyl"]
> nrow(mtcars) # number of data rows
> ncol(mtcars) # number of columns
> help(mtcars)
Defining a data frame
● > name <- c(“Nadiah”, “Nazri”)
● > age <- c(19, 23)
● > df <- data.frame (name, age)
● > df

Indexing and reassigning


● > df$name
● > df$name [2]
● > df [1, ]
> df [1, 1] <- 20
● > df$name <- paste(“Mr/Ms”, df$name)
Concatenating a data frame
● # add row (element)
● > df <- rbind (df, c(“Beh”, 24))

● # add column (attributes)


● > df$hobby <- c(“eating”, “sleeping”,
“shopping”)

Merging multiple data frames


● > df2 <- data.frame(
● name = c(“Hakim”, “Beh”, “Nazri”),
● hometown = c(“JB”, “KB”, “KT”)
● )
● > merge (df, df2)
Filtering, subsetting data frames
● > df$age < 21
● > df_sub <- df [ (df$age < 21), ]

● or simpler:

● > df_sub <- subset(df, age < 21)

Exercise

Using the built in data frame in R called mtcars: Motor Trend Car
Road Tests and logic operations, filter the data frame such that
the remaining data frame are car models that meet the following
conditions:
a) gas mileage is higher than the average of all the car models
in the dataset, AND
b) weighs lighter than the average weight of all the car models
in the dataset.
Data frame operations
At the end of this session, you will be able to perform
the following operations on data frames:
● Definition
● Indexing and reassigning
● Concatenation
● Merging
● Filtering

You might also like