Hello World! I, Aniq Kaw (Nick) would like to guide you through on a journey to understand what Data Frame is all about.
Let’s explore, shall we?

-LINKS WITHIN SECTION-

Section 1: Brief Introduction on Data Frame

Data frame is a two dimensional data or array-like structure in R whereby each component are equal in lengths. Its characteristics are as follows:-
  • Column names are non-empty.
  • Row names are unique.
  • Data stored includes common types such as numeric, character, factor, date, etc.
  • Every column must have a determined data type since single column can never have different data types.

Section 2: Creating Data Frame

Data Source: Acquired a small sample of Socar dataset in csv file format on some key metrices. It contains 7 rows of observations and 8 columns of variables consisting of different data types.
Method 1: Read and import the csv file
socar <- read.csv(file = 'Socar Data.csv')
socar
##   Renter_ID First_Name Second_Name Age   Car_Model Reservation_Date Days  Price
## 1     30120      Rahil       Ahmed  27        Axia       2020-10-06  3.0  210.0
## 2     20185       Hoon         Seo  38      Passat       2020-09-21  2.5 1231.0
## 3     17852        How     Wee Lee  26         BMW       2020-04-03  1.0  598.8
## 4     44325      Terry        Kuan  26        Myvi       2020-07-16 12.0  900.0
## 5     23596     Jaclyn         Lim  24    Vellfire       2020-02-29  1.0  429.3
## 6     38193   Kousalya       Manju  31 Mini Cooper       2020-11-05  4.5 1994.4
## 7     61407        Nur     Hidayah  47 Ford Ranger       2020-01-08  6.5 3432.0
Method 2: Create a vector with the exact information and transform it into data frame
Renter_ID = c(30120, 20185, 17852, 44325, 23596, 38193, 61407)
First_Name = c("Rahil", "Hoon", "How", "Terry", "Jaclyn", "Kousalya", "Nur")
Second_Name = c("Ahmed", "Seo", "Wee Lee", "Kuan", "Lim", "Manju", "Hidayah")
Age = c(27, 38, 26, 26, 24, 31, 47)
Car_Model = c("Axia", "Passat", "BMW", "Myvi", "Vellfire", "Mini Cooper", "Ford Ranger")
Reservation_Date = c("2020-10-06", "2020-09-21", "2020-04-03", "2020-07-16", "2020-02-29", "2020-11-05", "2020-01-08")
Days = c(3.0, 2.5, 1.0, 12.0, 1.0, 4.5, 6.5)
Price = c(210.0, 1231.0, 598.8, 900.0, 429.3, 1994.4, 3432.0)

socar1 <- data.frame(Renter_ID, First_Name, Second_Name, Age, Car_Model, Reservation_Date, Days, Price, stringsAsFactors = FALSE)
socar1
##   Renter_ID First_Name Second_Name Age   Car_Model Reservation_Date Days  Price
## 1     30120      Rahil       Ahmed  27        Axia       2020-10-06  3.0  210.0
## 2     20185       Hoon         Seo  38      Passat       2020-09-21  2.5 1231.0
## 3     17852        How     Wee Lee  26         BMW       2020-04-03  1.0  598.8
## 4     44325      Terry        Kuan  26        Myvi       2020-07-16 12.0  900.0
## 5     23596     Jaclyn         Lim  24    Vellfire       2020-02-29  1.0  429.3
## 6     38193   Kousalya       Manju  31 Mini Cooper       2020-11-05  4.5 1994.4
## 7     61407        Nur     Hidayah  47 Ford Ranger       2020-01-08  6.5 3432.0

Section 3: Data Processing

Showing a few steps using different functions to clean and transform a raw data into processed data.
Step 1: Merge “First_Name” and “Second_Name” column into one column, “Name” via string concatenation.
socar$Name <- paste(socar$First_Name, socar$Second_Name)
socar
##   Renter_ID First_Name Second_Name Age   Car_Model Reservation_Date Days  Price
## 1     30120      Rahil       Ahmed  27        Axia       2020-10-06  3.0  210.0
## 2     20185       Hoon         Seo  38      Passat       2020-09-21  2.5 1231.0
## 3     17852        How     Wee Lee  26         BMW       2020-04-03  1.0  598.8
## 4     44325      Terry        Kuan  26        Myvi       2020-07-16 12.0  900.0
## 5     23596     Jaclyn         Lim  24    Vellfire       2020-02-29  1.0  429.3
## 6     38193   Kousalya       Manju  31 Mini Cooper       2020-11-05  4.5 1994.4
## 7     61407        Nur     Hidayah  47 Ford Ranger       2020-01-08  6.5 3432.0
##             Name
## 1    Rahil Ahmed
## 2       Hoon Seo
## 3    How Wee Lee
## 4     Terry Kuan
## 5     Jaclyn Lim
## 6 Kousalya Manju
## 7    Nur Hidayah
Step 2: Remove “First_Name” and “Second_Name” columns (further details in Section 9).
socar <- socar[,c(-2,-3)]
socar
##   Renter_ID Age   Car_Model Reservation_Date Days  Price           Name
## 1     30120  27        Axia       2020-10-06  3.0  210.0    Rahil Ahmed
## 2     20185  38      Passat       2020-09-21  2.5 1231.0       Hoon Seo
## 3     17852  26         BMW       2020-04-03  1.0  598.8    How Wee Lee
## 4     44325  26        Myvi       2020-07-16 12.0  900.0     Terry Kuan
## 5     23596  24    Vellfire       2020-02-29  1.0  429.3     Jaclyn Lim
## 6     38193  31 Mini Cooper       2020-11-05  4.5 1994.4 Kousalya Manju
## 7     61407  47 Ford Ranger       2020-01-08  6.5 3432.0    Nur Hidayah
Step 3: Rearrange columns.
socar <- socar[,c(1,7,2,3,4,5,6)]
socar
##   Renter_ID           Name Age   Car_Model Reservation_Date Days  Price
## 1     30120    Rahil Ahmed  27        Axia       2020-10-06  3.0  210.0
## 2     20185       Hoon Seo  38      Passat       2020-09-21  2.5 1231.0
## 3     17852    How Wee Lee  26         BMW       2020-04-03  1.0  598.8
## 4     44325     Terry Kuan  26        Myvi       2020-07-16 12.0  900.0
## 5     23596     Jaclyn Lim  24    Vellfire       2020-02-29  1.0  429.3
## 6     38193 Kousalya Manju  31 Mini Cooper       2020-11-05  4.5 1994.4
## 7     61407    Nur Hidayah  47 Ford Ranger       2020-01-08  6.5 3432.0
Step 4: Convert data type “Reservation_Date” to date and data type “Renter_ID” to character using explicit coercion.
socar$Reservation_Date <- as.Date(socar$Reservation_Date, "%Y-%m-%d")
socar$Renter_ID <- as.character(socar$Renter_ID)
socar
##   Renter_ID           Name Age   Car_Model Reservation_Date Days  Price
## 1     30120    Rahil Ahmed  27        Axia       2020-10-06  3.0  210.0
## 2     20185       Hoon Seo  38      Passat       2020-09-21  2.5 1231.0
## 3     17852    How Wee Lee  26         BMW       2020-04-03  1.0  598.8
## 4     44325     Terry Kuan  26        Myvi       2020-07-16 12.0  900.0
## 5     23596     Jaclyn Lim  24    Vellfire       2020-02-29  1.0  429.3
## 6     38193 Kousalya Manju  31 Mini Cooper       2020-11-05  4.5 1994.4
## 7     61407    Nur Hidayah  47 Ford Ranger       2020-01-08  6.5 3432.0
Step 5: Sort rows by the most recent “Reservation_Date”.
socar <- socar[order(Reservation_Date, decreasing=TRUE),]  
socar
##   Renter_ID           Name Age   Car_Model Reservation_Date Days  Price
## 6     38193 Kousalya Manju  31 Mini Cooper       2020-11-05  4.5 1994.4
## 1     30120    Rahil Ahmed  27        Axia       2020-10-06  3.0  210.0
## 2     20185       Hoon Seo  38      Passat       2020-09-21  2.5 1231.0
## 4     44325     Terry Kuan  26        Myvi       2020-07-16 12.0  900.0
## 3     17852    How Wee Lee  26         BMW       2020-04-03  1.0  598.8
## 5     23596     Jaclyn Lim  24    Vellfire       2020-02-29  1.0  429.3
## 7     61407    Nur Hidayah  47 Ford Ranger       2020-01-08  6.5 3432.0

Section 4: Data Checking 

Step 1: Check whether it is a data frame.
is.data.frame(socar)
## [1] TRUE
Step 2: Check structure of data frame.
str(socar)  
## 'data.frame':    7 obs. of  7 variables:
##  $ Renter_ID       : chr  "38193" "30120" "20185" "44325" ...
##  $ Name            : chr  "Kousalya Manju" "Rahil Ahmed" "Hoon Seo" "Terry Kuan" ...
##  $ Age             : int  31 27 38 26 26 24 47
##  $ Car_Model       : chr  "Mini Cooper" "Axia" "Passat" "Myvi" ...
##  $ Reservation_Date: Date, format: "2020-11-05" "2020-10-06" ...
##  $ Days            : num  4.5 3 2.5 12 1 1 6.5
##  $ Price           : num  1994 210 1231 900 599 ...
Step 3: Provide summary of data frame.
summary(socar)
##   Renter_ID             Name                Age         Car_Model        
##  Length:7           Length:7           Min.   :24.00   Length:7          
##  Class :character   Class :character   1st Qu.:26.00   Class :character  
##  Mode  :character   Mode  :character   Median :27.00   Mode  :character  
##                                        Mean   :31.29                     
##                                        3rd Qu.:34.50                     
##                                        Max.   :47.00                     
##  Reservation_Date          Days            Price     
##  Min.   :2020-01-08   Min.   : 1.000   Min.   : 210  
##  1st Qu.:2020-03-17   1st Qu.: 1.750   1st Qu.: 514  
##  Median :2020-07-16   Median : 3.000   Median : 900  
##  Mean   :2020-06-21   Mean   : 4.357   Mean   :1256  
##  3rd Qu.:2020-09-28   3rd Qu.: 5.500   3rd Qu.:1613  
##  Max.   :2020-11-05   Max.   :12.000   Max.   :3432
Insights:-
  • The data frame contains 7 observations and 7 variables.
  • Character data type: Renter_ID, Name, and Car_Model variables
  • Integer data type: Age variable
  • Numeric data type: Days and Price variables
  • Date data type: Reservation_Date

Section 5: Data Display

Load package kableExtra and utilize function kable to view data frames in a more readable, tabular format.
library(kableExtra)
knitr::kable(socar[, 1:7], "simple", align = "clclcrr", bootstrap_options = "striped", font_size = 10, full_width= F, caption = "Table 1: Socar Data.")
Table 1: Socar Data.
Renter_ID Name Age Car_Model Reservation_Date Days Price
6 38193 Kousalya Manju 31 Mini Cooper 2020-11-05 4.5 1994.4
1 30120 Rahil Ahmed 27 Axia 2020-10-06 3.0 210.0
2 20185 Hoon Seo 38 Passat 2020-09-21 2.5 1231.0
4 44325 Terry Kuan 26 Myvi 2020-07-16 12.0 900.0
3 17852 How Wee Lee 26 BMW 2020-04-03 1.0 598.8
5 23596 Jaclyn Lim 24 Vellfire 2020-02-29 1.0 429.3
7 61407 Nur Hidayah 47 Ford Ranger 2020-01-08 6.5 3432.0
socar
##   Renter_ID           Name Age   Car_Model Reservation_Date Days  Price
## 6     38193 Kousalya Manju  31 Mini Cooper       2020-11-05  4.5 1994.4
## 1     30120    Rahil Ahmed  27        Axia       2020-10-06  3.0  210.0
## 2     20185       Hoon Seo  38      Passat       2020-09-21  2.5 1231.0
## 4     44325     Terry Kuan  26        Myvi       2020-07-16 12.0  900.0
## 3     17852    How Wee Lee  26         BMW       2020-04-03  1.0  598.8
## 5     23596     Jaclyn Lim  24    Vellfire       2020-02-29  1.0  429.3
## 7     61407    Nur Hidayah  47 Ford Ranger       2020-01-08  6.5 3432.0

Section 6: Functions of Data Frame 

ncol(): Compute number of columns.
ncol(socar)
## [1] 7
nrow(): Compute number of rows.
nrow(socar)
## [1] 7
length(): Similar to ncol().
length(socar)
## [1] 7
names(): Access column names.
names(socar)
## [1] "Renter_ID"        "Name"             "Age"              "Car_Model"       
## [5] "Reservation_Date" "Days"             "Price"
row.names(): Access row names.
row.names(socar)
## [1] "6" "1" "2" "4" "3" "5" "7"
Utilize names() to change column names via 2 methods:-
  1. Use one column name.
names(socar)[names(socar) == 'Renter_ID'] <- 'ID'
socar
##      ID           Name Age   Car_Model Reservation_Date Days  Price
## 6 38193 Kousalya Manju  31 Mini Cooper       2020-11-05  4.5 1994.4
## 1 30120    Rahil Ahmed  27        Axia       2020-10-06  3.0  210.0
## 2 20185       Hoon Seo  38      Passat       2020-09-21  2.5 1231.0
## 4 44325     Terry Kuan  26        Myvi       2020-07-16 12.0  900.0
## 3 17852    How Wee Lee  26         BMW       2020-04-03  1.0  598.8
## 5 23596     Jaclyn Lim  24    Vellfire       2020-02-29  1.0  429.3
## 7 61407    Nur Hidayah  47 Ford Ranger       2020-01-08  6.5 3432.0
  1. Use vectors on all column names.
names(socar) <- c("ID","Name", "Age", "Car Model", "Booking Date", "Days", "Price")
socar
##      ID           Name Age   Car Model Booking Date Days  Price
## 6 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## 1 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## 2 20185       Hoon Seo  38      Passat   2020-09-21  2.5 1231.0
## 4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0  900.0
## 3 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## 5 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
## 7 61407    Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0
Utilize row.names() to change row names.
row.names(socar) <- c("S1", "S2", "S3", "S4", "S5", "S6", "S7")
socar 
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## S3 20185       Hoon Seo  38      Passat   2020-09-21  2.5 1231.0
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0  900.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
## S7 61407    Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0

Section 7: Accessing Components of Data Frame (Single row or column)

socar
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## S3 20185       Hoon Seo  38      Passat   2020-09-21  2.5 1231.0
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0  900.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
## S7 61407    Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0

Access in a List Style:

Returns column result in a form of data frame.
socar["Age"]
##    Age
## S1  31
## S2  27
## S3  38
## S4  26
## S5  26
## S6  24
## S7  47
class(socar["Age"])
## [1] "data.frame"
Returns column results in a form of vector via 3 methods:-
  1. Use $ operator.
socar$Age
## [1] 31 27 38 26 26 24 47
  1. Use [[ ]] operator with column name.
socar[["Age"]]
## [1] 31 27 38 26 26 24 47
  1. Use [[ ]] operator with column index number.
socar[[3]]
## [1] 31 27 38 26 26 24 47
class(socar[["Age"]])
## [1] "integer"
Returns row result in a form of data frame.
socar["S1",]
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
Access an element via 3 methods:-
  1. Use $ operator.
socar$Age[3]
## [1] 38
  1. Use [[ ]] operator with column name.
socar[["Age"]][3]
## [1] 38
  1. Use [[ ]] operator with column index number.
socar[[3]][3]
## [1] 38

Access in a Matrix Style:

Returns row result in a form of data frame.
  • class(): indicates the outer structure or dimensions built from vectors or lists.
  • typeof(): indicates the inner element or its fundamentals.
socar[5,]
##       ID        Name Age Car Model Booking Date Days Price
## S5 17852 How Wee Lee  26       BMW   2020-04-03    1 598.8
class(socar[5,])
## [1] "data.frame"
typeof(socar[5,])
## [1] "list"
Returns column result in a form of vector.
socar[,4]
## [1] "Mini Cooper" "Axia"        "Passat"      "Myvi"        "BMW"        
## [6] "Vellfire"    "Ford Ranger"
class(socar[,4])
## [1] "character"
Returns column result in a form of data frame.
socar[,4, drop = FALSE]
##      Car Model
## S1 Mini Cooper
## S2        Axia
## S3      Passat
## S4        Myvi
## S5         BMW
## S6    Vellfire
## S7 Ford Ranger
class(socar[,4, drop = FALSE])
## [1] "data.frame"
Access an element.
socar[5,4]
## [1] "BMW"

Section 8: Accessing Components of Data Frame (Multiple rows or columns)

socar
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## S3 20185       Hoon Seo  38      Passat   2020-09-21  2.5 1231.0
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0  900.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
## S7 61407    Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0
Access any 2 rows by row index number.
socar[c(1,5),]
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
Access all rows except row 3 and row 6 by row index number.
socar[c(-3, -6),]
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0  900.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S7 61407    Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0
Access any 4 rows using logical operators.
socar[c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE),]
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S3 20185       Hoon Seo  38      Passat   2020-09-21  2.5 1231.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S7 61407    Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0
Access any 2 columns by column name.
socar[,c("Car Model", "Price")]
##      Car Model  Price
## S1 Mini Cooper 1994.4
## S2        Axia  210.0
## S3      Passat 1231.0
## S4        Myvi  900.0
## S5         BMW  598.8
## S6    Vellfire  429.3
## S7 Ford Ranger 3432.0
Access any 2 rows using row name and any 3 consecutive column.
socar[c("S1", "S4"), 3:5]
##    Age   Car Model Booking Date
## S1  31 Mini Cooper   2020-11-05
## S4  26        Myvi   2020-07-16
Access columns by partial matching.

$Na is similar to $Name

socar$Na
## [1] "Kousalya Manju" "Rahil Ahmed"    "Hoon Seo"       "Terry Kuan"    
## [5] "How Wee Lee"    "Jaclyn Lim"     "Nur Hidayah"
Use head() function to access first 6 rows.
head(socar)
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## S3 20185       Hoon Seo  38      Passat   2020-09-21  2.5 1231.0
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0  900.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
Use tail() function to access last 4 rows.
tail(socar, 4)
##       ID        Name Age   Car Model Booking Date Days  Price
## S4 44325  Terry Kuan  26        Myvi   2020-07-16 12.0  900.0
## S5 17852 How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S6 23596  Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
## S7 61407 Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0
Extract data with 1 condition.

Renters aged 26.

socar[socar$Age == 26,]
##       ID        Name Age Car Model Booking Date Days Price
## S4 44325  Terry Kuan  26      Myvi   2020-07-16   12 900.0
## S5 17852 How Wee Lee  26       BMW   2020-04-03    1 598.8
Extract data with 1 condition using subset function.

Renters aged 26.

subset(socar, Age == 26)
##       ID        Name Age Car Model Booking Date Days Price
## S4 44325  Terry Kuan  26      Myvi   2020-07-16   12 900.0
## S5 17852 How Wee Lee  26       BMW   2020-04-03    1 598.8
Extract data with 2 conditions using ‘OR’ logical operator.

Renters who booked a Passat or cars worth more than RM1000.

socar[socar$"Car Model" == "Passat" | socar$Price > 1000,]
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S3 20185       Hoon Seo  38      Passat   2020-09-21  2.5 1231.0
## S7 61407    Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0
Extract data with 2 conditions using ‘AND’ logical operator.

Renters who are below 30 years old and booked a car before 30th June 2020.

socar[socar$Age < 30 & socar$"Booking Date" < "2020-06-30",]
##       ID        Name Age Car Model Booking Date Days Price
## S5 17852 How Wee Lee  26       BMW   2020-04-03    1 598.8
## S6 23596  Jaclyn Lim  24  Vellfire   2020-02-29    1 429.3

Section 9: Modifying, Adding, or Removing Components of Data Frame

socar
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## S3 20185       Hoon Seo  38      Passat   2020-09-21  2.5 1231.0
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0  900.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
## S7 61407    Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0
Modify value via reassignment.

Changing Terry Kuan’s expenditure from RM900 to RM1200.

socar[4,"Price"] <- 1200
socar
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  31 Mini Cooper   2020-11-05  4.5 1994.4
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## S3 20185       Hoon Seo  38      Passat   2020-09-21  2.5 1231.0
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0 1200.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
## S7 61407    Nur Hidayah  47 Ford Ranger   2020-01-08  6.5 3432.0
Replace value.

Replacing all renters above age 30 to an age value of 40.

socar[socar$"Age" > 30, 3] <- 40
socar
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  40 Mini Cooper   2020-11-05  4.5 1994.4
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## S3 20185       Hoon Seo  40      Passat   2020-09-21  2.5 1231.0
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0 1200.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
## S7 61407    Nur Hidayah  40 Ford Ranger   2020-01-08  6.5 3432.0
Add components to data frame via 3 methods:-
  1. rbind(): Add a new row of renter with his details.
socar <- rbind(socar, list(70426, "Aniq Kaw", 25, "Bezza", "2020-11-11", 3, 240))
socar
##       ID           Name Age   Car Model Booking Date Days  Price
## S1 38193 Kousalya Manju  40 Mini Cooper   2020-11-05  4.5 1994.4
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0
## S3 20185       Hoon Seo  40      Passat   2020-09-21  2.5 1231.0
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0 1200.0
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3
## S7 61407    Nur Hidayah  40 Ford Ranger   2020-01-08  6.5 3432.0
## 8  70426       Aniq Kaw  25       Bezza   2020-11-11  3.0  240.0
  1. cbind(): Add a new column, “Country”.
socar <- cbind(socar, Country = c("MY", "IN", "KR", "CN", "US", "HK", "MY", "UK"))
socar
##       ID           Name Age   Car Model Booking Date Days  Price Country
## S1 38193 Kousalya Manju  40 Mini Cooper   2020-11-05  4.5 1994.4      MY
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0      IN
## S3 20185       Hoon Seo  40      Passat   2020-09-21  2.5 1231.0      KR
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0 1200.0      CN
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8      US
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3      HK
## S7 61407    Nur Hidayah  40 Ford Ranger   2020-01-08  6.5 3432.0      MY
## 8  70426       Aniq Kaw  25       Bezza   2020-11-11  3.0  240.0      UK
  1. $ operator: Add a new column, “Gender” with data type, factor. Factors are usually used to represent categorical data.
socar$Gender <- factor(c("F", "M", "M", "M", "M", "F", "F", "M"), levels = c("M", "F", "Not sure"))
socar
##       ID           Name Age   Car Model Booking Date Days  Price Country Gender
## S1 38193 Kousalya Manju  40 Mini Cooper   2020-11-05  4.5 1994.4      MY      F
## S2 30120    Rahil Ahmed  27        Axia   2020-10-06  3.0  210.0      IN      M
## S3 20185       Hoon Seo  40      Passat   2020-09-21  2.5 1231.0      KR      M
## S4 44325     Terry Kuan  26        Myvi   2020-07-16 12.0 1200.0      CN      M
## S5 17852    How Wee Lee  26         BMW   2020-04-03  1.0  598.8      US      M
## S6 23596     Jaclyn Lim  24    Vellfire   2020-02-29  1.0  429.3      HK      F
## S7 61407    Nur Hidayah  40 Ford Ranger   2020-01-08  6.5 3432.0      MY      F
## 8  70426       Aniq Kaw  25       Bezza   2020-11-11  3.0  240.0      UK      M
class(socar$Gender)
## [1] "factor"
Remove components on data frame via 2 methods:-
  1. $ operator for columns.
socar$ID <- NULL
socar$Age <- NULL
socar
##              Name   Car Model Booking Date Days  Price Country Gender
## S1 Kousalya Manju Mini Cooper   2020-11-05  4.5 1994.4      MY      F
## S2    Rahil Ahmed        Axia   2020-10-06  3.0  210.0      IN      M
## S3       Hoon Seo      Passat   2020-09-21  2.5 1231.0      KR      M
## S4     Terry Kuan        Myvi   2020-07-16 12.0 1200.0      CN      M
## S5    How Wee Lee         BMW   2020-04-03  1.0  598.8      US      M
## S6     Jaclyn Lim    Vellfire   2020-02-29  1.0  429.3      HK      F
## S7    Nur Hidayah Ford Ranger   2020-01-08  6.5 3432.0      MY      F
## 8        Aniq Kaw       Bezza   2020-11-11  3.0  240.0      UK      M
  1. Use index via reassignment for rows.
socar <- socar[-4,]
socar
##              Name   Car Model Booking Date Days  Price Country Gender
## S1 Kousalya Manju Mini Cooper   2020-11-05  4.5 1994.4      MY      F
## S2    Rahil Ahmed        Axia   2020-10-06  3.0  210.0      IN      M
## S3       Hoon Seo      Passat   2020-09-21  2.5 1231.0      KR      M
## S5    How Wee Lee         BMW   2020-04-03  1.0  598.8      US      M
## S6     Jaclyn Lim    Vellfire   2020-02-29  1.0  429.3      HK      F
## S7    Nur Hidayah Ford Ranger   2020-01-08  6.5 3432.0      MY      F
## 8        Aniq Kaw       Bezza   2020-11-11  3.0  240.0      UK      M

Section 10: Miscellaneous 

Calculation: Add value to an existing variable.

Extra charges causing price to increase by RM100.

socar["Price"] + 100
##     Price
## S1 2094.4
## S2  310.0
## S3 1331.0
## S5  698.8
## S6  529.3
## S7 3532.0
## 8   340.0
Calculation: Add new variable by using formula between 2 variables.

Find price per day (PPD) for each renter.

socar <- transform(socar, PPD = Price / Days)
socar
##              Name   Car.Model Booking.Date Days  Price Country Gender   PPD
## S1 Kousalya Manju Mini Cooper   2020-11-05  4.5 1994.4      MY      F 443.2
## S2    Rahil Ahmed        Axia   2020-10-06  3.0  210.0      IN      M  70.0
## S3       Hoon Seo      Passat   2020-09-21  2.5 1231.0      KR      M 492.4
## S5    How Wee Lee         BMW   2020-04-03  1.0  598.8      US      M 598.8
## S6     Jaclyn Lim    Vellfire   2020-02-29  1.0  429.3      HK      F 429.3
## S7    Nur Hidayah Ford Ranger   2020-01-08  6.5 3432.0      MY      F 528.0
## 8        Aniq Kaw       Bezza   2020-11-11  3.0  240.0      UK      M  80.0
unique(): Find unique element in a column.
unique(socar$Gender)
## [1] F M
## Levels: M F Not sure
t(): Transpose data frame.
t(socar)
##              S1               S2            S3           S5           
## Name         "Kousalya Manju" "Rahil Ahmed" "Hoon Seo"   "How Wee Lee"
## Car.Model    "Mini Cooper"    "Axia"        "Passat"     "BMW"        
## Booking.Date "2020-11-05"     "2020-10-06"  "2020-09-21" "2020-04-03" 
## Days         "4.5"            "3.0"         "2.5"        "1.0"        
## Price        "1994.4"         " 210.0"      "1231.0"     " 598.8"     
## Country      "MY"             "IN"          "KR"         "US"         
## Gender       "F"              "M"           "M"          "M"          
## PPD          "443.2"          " 70.0"       "492.4"      "598.8"      
##              S6           S7            8           
## Name         "Jaclyn Lim" "Nur Hidayah" "Aniq Kaw"  
## Car.Model    "Vellfire"   "Ford Ranger" "Bezza"     
## Booking.Date "2020-02-29" "2020-01-08"  "2020-11-11"
## Days         "1.0"        "6.5"         "3.0"       
## Price        " 429.3"     "3432.0"      " 240.0"    
## Country      "HK"         "MY"          "UK"        
## Gender       "F"          "F"           "M"         
## PPD          "429.3"      "528.0"       " 80.0"
as.list(): Convert to list format.
as.list(socar)
## $Name
## [1] "Kousalya Manju" "Rahil Ahmed"    "Hoon Seo"       "How Wee Lee"   
## [5] "Jaclyn Lim"     "Nur Hidayah"    "Aniq Kaw"      
## 
## $Car.Model
## [1] "Mini Cooper" "Axia"        "Passat"      "BMW"         "Vellfire"   
## [6] "Ford Ranger" "Bezza"      
## 
## $Booking.Date
## [1] "2020-11-05" "2020-10-06" "2020-09-21" "2020-04-03" "2020-02-29"
## [6] "2020-01-08" "2020-11-11"
## 
## $Days
## [1] 4.5 3.0 2.5 1.0 1.0 6.5 3.0
## 
## $Price
## [1] 1994.4  210.0 1231.0  598.8  429.3 3432.0  240.0
## 
## $Country
## [1] "MY" "IN" "KR" "US" "HK" "MY" "UK"
## 
## $Gender
## [1] F M M M F F M
## Levels: M F Not sure
## 
## $PPD
## [1] 443.2  70.0 492.4 598.8 429.3 528.0  80.0
cat(toJSON()): Convert to JSON file format which is useful for providing output in user defined functions.
library("jsonlite")
cat(toJSON(socar))
## [{"Name":"Kousalya Manju","Car.Model":"Mini Cooper","Booking.Date":"2020-11-05","Days":4.5,"Price":1994.4,"Country":"MY","Gender":"F","PPD":443.2,"_row":"S1"},{"Name":"Rahil Ahmed","Car.Model":"Axia","Booking.Date":"2020-10-06","Days":3,"Price":210,"Country":"IN","Gender":"M","PPD":70,"_row":"S2"},{"Name":"Hoon Seo","Car.Model":"Passat","Booking.Date":"2020-09-21","Days":2.5,"Price":1231,"Country":"KR","Gender":"M","PPD":492.4,"_row":"S3"},{"Name":"How Wee Lee","Car.Model":"BMW","Booking.Date":"2020-04-03","Days":1,"Price":598.8,"Country":"US","Gender":"M","PPD":598.8,"_row":"S5"},{"Name":"Jaclyn Lim","Car.Model":"Vellfire","Booking.Date":"2020-02-29","Days":1,"Price":429.3,"Country":"HK","Gender":"F","PPD":429.3,"_row":"S6"},{"Name":"Nur Hidayah","Car.Model":"Ford Ranger","Booking.Date":"2020-01-08","Days":6.5,"Price":3432,"Country":"MY","Gender":"F","PPD":528,"_row":"S7"},{"Name":"Aniq Kaw","Car.Model":"Bezza","Booking.Date":"2020-11-11","Days":3,"Price":240,"Country":"UK","Gender":"M","PPD":80,"_row":"8"}]

Signing off

-Aniq Kaw (Nick)-