Write a simple R Markdown to explain some of the R codes you have learned regarding data frame. You are free to write your own program and add new codes such as how would you show all the rows except the last one or how would you get the last 6 rows of the data frame. Eg. you can explain what a data frame is, and then write the code to show how R handles data frame, and so on.But one mandatory topic is you must explain the different ways R returns back a vector or a data frame when you access values from a data frame. Your program doesnโt have to be long. Play around with the different font sizes in markdown to make your markdown readable. Explore. Publish your markdown on RPubs and submit the link only. Adding new codes that have not been discussed will earn you high marks.
Best to create your own data frame or use a data frame which is small in size and so that it would be easy to see the results after the execution of each or after a few codes.
##Create a vector to store information on people of Hogwarts from Harry Potter universe
name <- c("Albus Dumbledore","Harry Potter","Hermione Granger","Ron Weasley","Draco Malfoy","Severus Snape","Cho Chang","Cedric Diggory", "Luna Lovegood")
housename <- c("Gryffindor","Gryffindor","Gryffindor","Gryffindor","Slytherin","Slytherin","Ravenclaw","Hufflepuff","Ravenclaw")
gender <-c("Male","Male","Female","Male","Male","Male","Female","Male","Female")
age <-c(115,11,12,11,12,28,14,16,10)
year <-c(NA,1,1,1,1,NA,3,4,0)
is.student <- c(FALSE,TRUE,TRUE,TRUE,TRUE,FALSE,TRUE,TRUE,TRUE)
##From the vector of information, store the details into a data frame
##Data frame is a list of vectors with equal length
hogwarts <- data.frame(name, housename, gender, age, year, is.student)
print(hogwarts)
## name housename gender age year is.student
## 1 Albus Dumbledore Gryffindor Male 115 NA FALSE
## 2 Harry Potter Gryffindor Male 11 1 TRUE
## 3 Hermione Granger Gryffindor Female 12 1 TRUE
## 4 Ron Weasley Gryffindor Male 11 1 TRUE
## 5 Draco Malfoy Slytherin Male 12 1 TRUE
## 6 Severus Snape Slytherin Male 28 NA FALSE
## 7 Cho Chang Ravenclaw Female 14 3 TRUE
## 8 Cedric Diggory Hufflepuff Male 16 4 TRUE
## 9 Luna Lovegood Ravenclaw Female 10 0 TRUE
##Check the structure of the data frame created
str(hogwarts)
## 'data.frame': 9 obs. of 6 variables:
## $ name : chr "Albus Dumbledore" "Harry Potter" "Hermione Granger" "Ron Weasley" ...
## $ housename : chr "Gryffindor" "Gryffindor" "Gryffindor" "Gryffindor" ...
## $ gender : chr "Male" "Male" "Female" "Male" ...
## $ age : num 115 11 12 11 12 28 14 16 10
## $ year : num NA 1 1 1 1 NA 3 4 0
## $ is.student: logi FALSE TRUE TRUE TRUE TRUE FALSE ...
##Is this really is a data frame?
class(hogwarts)
## [1] "data.frame"
##What is the type of this data frame?
typeof(hogwarts)
## [1] "list"
##Check what are attributes of this data frame
attributes(hogwarts)
## $names
## [1] "name" "housename" "gender" "age" "year"
## [6] "is.student"
##
## $class
## [1] "data.frame"
##
## $row.names
## [1] 1 2 3 4 5 6 7 8 9
##Find out the dimension of this data frame
dim(hogwarts)
## [1] 9 6
##How many record are there in this data frame?
nrow(hogwarts)
## [1] 9
##How many attributes in this data frame?
ncol(hogwarts)
## [1] 6
##Retrieve the first record from this data frame?
print(hogwarts[1,])
## name housename gender age year is.student
## 1 Albus Dumbledore Gryffindor Male 115 NA FALSE
##What is the class of this retrieved record?
class(hogwarts[1,])
## [1] "data.frame"
#Force the retrieved record to return as character vector
class(as.character(hogwarts[1,]))
## [1] "character"
##Since data frame is made of list, you can also do unlist to retrieve record as vector
is.vector(unlist(hogwarts[1,]))
## [1] TRUE
str(unlist(hogwarts[1,]))
## Named chr [1:6] "Albus Dumbledore" "Gryffindor" "Male" "115" NA "FALSE"
## - attr(*, "names")= chr [1:6] "name" "housename" "gender" "age" ...
##Retrieve one of the attribute from the data frame and see the returned object
class(hogwarts$name)
## [1] "character"
##Another way to retrieve the column. Both returned as vector
class(hogwarts[["name"]])
## [1] "character"
##Retrieve the last 2 record from this data frame and see the returned type
class(tail(hogwarts,2))
## [1] "data.frame"
##Retrieve the 3rd to 5th record with 2 attributes only from this data frame
print(hogwarts[3:5,c("name","housename")])
## name housename
## 3 Hermione Granger Gryffindor
## 4 Ron Weasley Gryffindor
## 5 Draco Malfoy Slytherin
##Subset the data frame by dropping the year column
hogwarts.no.year <- subset(hogwarts, select = - year)
##See the class of the returned subset
class(hogwarts.no.year)
## [1] "data.frame"
##Create new record with data frame type
new_record_df <- data.frame("Ginny Weasley","Gryffindor","Female",10,TRUE)
##Change the column names to be same with subsetted data frame
names(new_record_df) <- c(names(hogwarts.no.year))
print(new_record_df)
## name housename gender age is.student
## 1 Ginny Weasley Gryffindor Female 10 TRUE
##Add the new single data frame into subsetted data frame
hogwarts.no.year <- rbind(hogwarts.no.year,new_record_df)
print(hogwarts.no.year)
## name housename gender age is.student
## 1 Albus Dumbledore Gryffindor Male 115 FALSE
## 2 Harry Potter Gryffindor Male 11 TRUE
## 3 Hermione Granger Gryffindor Female 12 TRUE
## 4 Ron Weasley Gryffindor Male 11 TRUE
## 5 Draco Malfoy Slytherin Male 12 TRUE
## 6 Severus Snape Slytherin Male 28 FALSE
## 7 Cho Chang Ravenclaw Female 14 TRUE
## 8 Cedric Diggory Hufflepuff Male 16 TRUE
## 9 Luna Lovegood Ravenclaw Female 10 TRUE
## 10 Ginny Weasley Gryffindor Female 10 TRUE
##Check the structure and class of the updated subset data frame
##The class of each variables still intact
str(hogwarts.no.year)
## 'data.frame': 10 obs. of 5 variables:
## $ name : chr "Albus Dumbledore" "Harry Potter" "Hermione Granger" "Ron Weasley" ...
## $ housename : chr "Gryffindor" "Gryffindor" "Gryffindor" "Gryffindor" ...
## $ gender : chr "Male" "Male" "Female" "Male" ...
## $ age : num 115 11 12 11 12 28 14 16 10 10
## $ is.student: logi FALSE TRUE TRUE TRUE TRUE FALSE ...
class(hogwarts.no.year)
## [1] "data.frame"
##Create a vector for new record
new_record_vector <- c("Nymphadora Tonks","Hufflepuff","Female",25,FALSE)
print(new_record_vector)
## [1] "Nymphadora Tonks" "Hufflepuff" "Female" "25"
## [5] "FALSE"
##Check the structure of the new vector record
str(new_record_vector)
## chr [1:5] "Nymphadora Tonks" "Hufflepuff" "Female" "25" "FALSE"
class(new_record_vector)
## [1] "character"
##Add the new vector record into the test subsetted data frame
hogwarts.no.year<-rbind(hogwarts.no.year,new_record_vector)
print(hogwarts.no.year)
## name housename gender age is.student
## 1 Albus Dumbledore Gryffindor Male 115 FALSE
## 2 Harry Potter Gryffindor Male 11 TRUE
## 3 Hermione Granger Gryffindor Female 12 TRUE
## 4 Ron Weasley Gryffindor Male 11 TRUE
## 5 Draco Malfoy Slytherin Male 12 TRUE
## 6 Severus Snape Slytherin Male 28 FALSE
## 7 Cho Chang Ravenclaw Female 14 TRUE
## 8 Cedric Diggory Hufflepuff Male 16 TRUE
## 9 Luna Lovegood Ravenclaw Female 10 TRUE
## 10 Ginny Weasley Gryffindor Female 10 TRUE
## 11 Nymphadora Tonks Hufflepuff Female 25 FALSE
##Notice how each attribute in the vector been coerced to character
##To prevent this is by converting the vector into data frame before bind
##Eg. by using as.data.frame function
str(hogwarts.no.year)
## 'data.frame': 11 obs. of 5 variables:
## $ name : chr "Albus Dumbledore" "Harry Potter" "Hermione Granger" "Ron Weasley" ...
## $ housename : chr "Gryffindor" "Gryffindor" "Gryffindor" "Gryffindor" ...
## $ gender : chr "Male" "Male" "Female" "Male" ...
## $ age : chr "115" "11" "12" "11" ...
## $ is.student: chr "FALSE" "TRUE" "TRUE" "TRUE" ...
class(hogwarts.no.year)
## [1] "data.frame"