R Markdown

This is an R Markdown document , for Lab Practise 4.

Data frames is an important type of data in R, used to store tabular data. It can be described as a list of vectors of equal length.

1.Creating a data frame:

  1. By reading a dataset
  1. By using data.frame(..) function
Week <- data.frame(SNo=1:7, Days =c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"))
print(Week)
##   SNo      Days
## 1   1    Monday
## 2   2   Tuesday
## 3   3 Wednesday
## 4   4  Thursday
## 5   5    Friday
## 6   6  Saturday
## 7   7    Sunday
ID = 1001:1005
Name = c("Anna Karenina", "1984", "Lolita", "Hamlet ", "The Great Gatsby")
Author = c("Leo Tolstoy","George Orwell","Vladimir Nabokov", "William Shakespeare","F. Scott Fizgerald")

Books = data.frame(ID, Name, Author)
print(Books)
##     ID             Name              Author
## 1 1001    Anna Karenina         Leo Tolstoy
## 2 1002             1984       George Orwell
## 3 1003           Lolita    Vladimir Nabokov
## 4 1004          Hamlet  William Shakespeare
## 5 1005 The Great Gatsby  F. Scott Fizgerald

2)Accessing the functions of data frame:

  1. names() - gets the column names of data frame
  2. nrow() - gets the number of rows in data frame
  3. ncol() - gets the number of columns in data frame
  4. Length() - gets the length of the data frame, same as ncol()
ColumnNames <-  names(Books)
print(ColumnNames)
## [1] "ID"     "Name"   "Author"
row.names(Books) <- c("Book1", "Book2", "Book3", "Book4", "Book5")
RowNames <- row.names(Books)
print(RowNames)
## [1] "Book1" "Book2" "Book3" "Book4" "Book5"
NoofRows <- nrow(Books)
print(NoofRows)
## [1] 5
NoofColumns <- ncol(Books)
print(NoofColumns)
## [1] 3
LengthofList <- length(Books)
print(LengthofList)
## [1] 3

3)Accessing the components of data frame:

  1. Accessing like a list, by using either [, [[, or $ operator to access the columns of the data frame. Lets find out the difference in their return types.
#Accessing a column in the data frame

c1 <- class(Books["Name"])
print(c1)
## [1] "data.frame"
c2 <- class(Books$Name)
print(c2)
## [1] "character"
c3 <- class(Books[["Name"]])
print(c3)
## [1] "character"
#Accessing an element in the column

element1 <- Books$Name[2]
print(element1)
## [1] "1984"
element2 <- Books[["Name"]][3]
print(element2)
## [1] "Lolita"
  1. Accessing like a matrix, by giving row and column index
#Returns the entire row as a data frame, with row name and column names
row1 <- Books[1,]
print(row1)
##         ID          Name      Author
## Book1 1001 Anna Karenina Leo Tolstoy
crow1 <- class(Books[1,])
print(crow1)
## [1] "data.frame"
#Returns the entire column as a vector
column1 <- Books[,1]
print(column1)
## [1] 1001 1002 1003 1004 1005
ccolumn1 <- class(Books[,1])
print(ccolumn1)
## [1] "integer"
#Returns a particular element as a vector
element3 <- Books[1,2]
print(element3)
## [1] "Anna Karenina"
celement3 <- class(Books[1,2])
print(celement3)
## [1] "character"
#Returns a particular element as a vector
element4 <- Books[1,2, drop=FALSE]
print(element4)
##                Name
## Book1 Anna Karenina
celement4 <- class(Books[1,2, drop=FALSE])
print(celement4)
## [1] "data.frame"

4)Modifying the components of data frame:

  1. Modifying a component
Books[5,3] <- "Mr.Scott Fizgerald"
print(Books)
##         ID             Name              Author
## Book1 1001    Anna Karenina         Leo Tolstoy
## Book2 1002             1984       George Orwell
## Book3 1003           Lolita    Vladimir Nabokov
## Book4 1004          Hamlet  William Shakespeare
## Book5 1005 The Great Gatsby  Mr.Scott Fizgerald
  1. Adding a component
Books <- rbind(Books, list(1006, "War and Peace", "Leo Tolstoy"))
print(Books)
##         ID             Name              Author
## Book1 1001    Anna Karenina         Leo Tolstoy
## Book2 1002             1984       George Orwell
## Book3 1003           Lolita    Vladimir Nabokov
## Book4 1004          Hamlet  William Shakespeare
## Book5 1005 The Great Gatsby  Mr.Scott Fizgerald
## 6     1006    War and Peace         Leo Tolstoy
Books <- cbind(Books, Price=c(125, 180,220,160,135,215))
print(Books)
##         ID             Name              Author Price
## Book1 1001    Anna Karenina         Leo Tolstoy   125
## Book2 1002             1984       George Orwell   180
## Book3 1003           Lolita    Vladimir Nabokov   220
## Book4 1004          Hamlet  William Shakespeare   160
## Book5 1005 The Great Gatsby  Mr.Scott Fizgerald   135
## 6     1006    War and Peace         Leo Tolstoy   215
Books$PublishedYear <- c(1877, 1949, 1955, 1603,1925, 1967)
print(Books)
##         ID             Name              Author Price PublishedYear
## Book1 1001    Anna Karenina         Leo Tolstoy   125          1877
## Book2 1002             1984       George Orwell   180          1949
## Book3 1003           Lolita    Vladimir Nabokov   220          1955
## Book4 1004          Hamlet  William Shakespeare   160          1603
## Book5 1005 The Great Gatsby  Mr.Scott Fizgerald   135          1925
## 6     1006    War and Peace         Leo Tolstoy   215          1967
  1. Removing a component
Books$PublishedYear <- NULL
print(Books)
##         ID             Name              Author Price
## Book1 1001    Anna Karenina         Leo Tolstoy   125
## Book2 1002             1984       George Orwell   180
## Book3 1003           Lolita    Vladimir Nabokov   220
## Book4 1004          Hamlet  William Shakespeare   160
## Book5 1005 The Great Gatsby  Mr.Scott Fizgerald   135
## 6     1006    War and Peace         Leo Tolstoy   215
Books <- Books[-6,]
str(Books)
## 'data.frame':    5 obs. of  4 variables:
##  $ ID    : num  1001 1002 1003 1004 1005
##  $ Name  : chr  "Anna Karenina" "1984" "Lolita" "Hamlet " ...
##  $ Author: chr  "Leo Tolstoy" "George Orwell" "Vladimir Nabokov" "William Shakespeare" ...
##  $ Price : num  125 180 220 160 135

End of the exercises in Practise Lab4