R Excercise Problem - I could NOT solve. Can anyone help?

Hello Experts,
I am learning R. In one of the online learning courses, I came across the following exercise that I could not resolve. Thought of picking your brain to solve this. Could someone help?

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")

Add a new column Carrier to hflights which will contain the actual Carrier name by referring abrCarrier and the UniqueCarrier column of hflights.
Use the glimpse(hflights) to view the values in the newly added column.

Thanks.

This looks like a homework question. Do not directly copy and paste instructions from your assignment here. Please read this guide:

Also, can you show us your attempts to solve the assignment? If you are stuck on a specific point we may be able to help you.

Have you been able to do any part of this ?
Have you succesfully installed and loaded the hflights package?

I have written the below code. And it gives result in R-Studio. However, I think, my approach is a bit convoluted. Moreover, I had to compile the code in webbased "katacoda" environment which is not liking my code and is not allowing me to progress to next chapter. Also, it could be my output is incorrect let me know. Thanks.

install.packages("dplyr")
install.packages("hflights")
library(dplyr)
library(hflights)

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")

df1 <- data.frame(abrCarrier)

#df1

#setwd("C:\Users\Dell\Documents\trial_1")

write.csv(df1,"abrCarrier_export.csv")

df2 <- read.delim("abrCarrier_export.csv",sep = ",")

names(df2) <- c("UniqueCarrier", "Carrier")

#df2

#library(hflights)

df0 <- hflights

hflights1 <- left_join (df0,df2, by = "UniqueCarrier")

glimpse(hflights1)

Could you edit your original post to replace this part with '[Course content removed]'?

# install.packages("hflights")
library(hflights)

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")

library(tidyverse)

#enframe to turn a list into a dataframe
abrCarrier_df <- enframe(abrCarrier,
                         name="UniqueCarrier",
                         value = "Carrier")
# 
# > abrCarrier_df
# # A tibble: 15 x 2
# UniqueCarrier Carrier           
# <chr>         <chr>             
# 1 AA            American          
# 2 AS            Alaska            
# 3 B6            JetBlue           
# 4 CO            Continental       
# 5 DL            Delta             
# 6 OO            SkyWest           
# 7 UA            United            
# 8 US            US_Airways        
# 9 WN            Southwest         
# 10 EV            Atlantic_Southeast
# 11 F9            Frontier          
# 12 FL            AirTran           
# 13 MQ            American_Eagle    
# 14 XE            ExpressJet        
# 15 YV            Mesa  

hflights1 <- left_join (hflights,abrCarrier_df, by = "UniqueCarrier")

glimpse(hflights1)
1 Like

Your code does solve the problem, except for an error generated by the last command -- the problem is that UniqueCarrier is a character column in hflights, but when you read in df2, the read.delim() made its UniqueCarrier into a factor column, which won't join with a character column. You could have used the argument stringsAsFactors = FALSE in your read.delim() call, but @nirgrahamuk's solution avoids this issue (and having to write and read back) by using enframe().

1 Like

Hello nirgrahamuk,

Thanks for your response. I tried to run the code. However, I am getting the following:

library(tidyverse)
Error in library(tidyverse) : there is no package called ‘tidyverse’

Do I have to install any package prior?

PS. Sorry for the silly question. I am brand new in R.

Thanks.

yes, any package that your system can't find, has not yet been installed on your device.
in your case running install.packages("tidyverse") should work fine, might be more complicated if you have macOS

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.