Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

library(dplyr, warn.

conflicts = FALSE)
dplyrs <- function(){

# print(getwd())
# print(list.files())

# Task 1:
hflights <- read.csv("hflights.csv")
print(dim(hflights))

# Task 2.
# Use the functions head () and tail () to take a look
print(head(hflights))
print(tail(hflights))

# Task 3:
print(head(hflights, 20 ))

# Task 4:
# glimpse () is like a transposed version of print.

glimpse(hflights)

# Task 5:
# Perform the following tasks:

# A. Create a data frame hflights1 which will have first fifty rows of the data
set hflights
d <- hflights[c(1:50),]

# B. Convert hflights1 into a tbl.


hflights1 <- as_tibble(d)

# C. To see how tbl behaves like data frames, save the UniqueCarrier column of
hflights tbl
# as an object named carriers, by using standard R syntax and print it.

carriers <- hflights1$UniqueCarrier


print(carriers)

# Task 6:
# Perform the following tasks:

# A. Create a list abrCarrier which will contain actual carrier names


corresponding to the values in the
# variable UniqueCarrier.

abrCarrier < - c("AA" = "American", "AS" = "Alaska", "B6" = "JetBlue", "CO" =


"Continental", "DL" = "Delta", "OO" = "SkyWest", "UA" = "United", "US" =
"US_Airways", "WN" = "Southwest", "EV" = "Atlantic_Southeast", "F9" = "Frontier",
"FL" = "AirTran", "MQ" = "American_Eagle", "XE" = "ExpressJet", "YV" = "Mesa")

# B. Add a new column Carrier to hflights which will contain the actual Carrier
name by referring abrCarrier and the UniqueCarrier column of hflights.

hflights <- transform( hflights, Carrier= ifelse(hflights$UniqueCarrier %in%


names(abrCarrier), abrCarrier[hflights$UniqueCarrier ], "Error"))
# C. Print the first 10 rows of the data set to view the values in the newly
added column.
print(head(hflights, 10))

dplyrs()

You might also like