Problems

Hone your R skills by doing problems.

Use R to produce a bar graph from this table:


freq <- c(5, 7, 2, 5, 3, 4)
names( freq ) <- c('India', 'China', 'Russia', 'Nigeria', 'Iran', 'Other')
barplot(freq, main = "This is a bar plot", xlab = "Countries", ylab = "Frequencies")

Data manipulation. Load the data ‘tornado.csv’.


Tornado Data

How many tornadoes occurred in AL (Alabama) and AR (Arkansas).

tornado <- read.csv('http://latul.be/mba_565/data/tornado.csv', 
    stringsAsFactors = FALSE)
attach( tornado )
# summing over TRUE and FALSE; R adds one to the sum for value TRUE
# `|` is the logical operator or.

sum(st=='AL' | st=='AR')
## [1] 941

How many tornadoes occurred in TX (Texas) before 2010.

sum( st=='TX' & yr < 2010 )
## [1] 451

Return the ‘mo’ (month), ‘yr’ (year), and ‘f’ (F-scale) for all tornadoes that occurred in DE (Delaware).

tornado[st == 'DE', c('mo', 'yr', 'f')]
##       mo   yr    f
## 6982   8 2011 EF-1
## 7963   9 2012 EF-0
## 8625   6 2013 EF-0
## 9322   5 2014 EF-1
## 10767  7 2015 EF-0

Arrange the tornadoes by date and time. What state had the most recent tornado?

list_var = c('date','time','st')
order_row = order(date, time)
tail( tornado[ order_row, list_var] )
##             date     time st
## 11136 2015-12-28 02:53:00 LA
## 11137 2015-12-28 03:20:00 LA
## 11138 2015-12-28 04:46:00 AR
## 11139 2015-12-28 05:43:00 MS
## 11140 2015-12-28 08:30:00 FL
## 11141 2015-12-28 15:58:00 NC

Get the count of tornadoes by year.

table(yr)
## yr
## 2007 2008 2009 2010 2011 2012 2013 2014 2015 
## 1116 1738 1182 1315 1777  956  939  906 1212

Add a new column, num_f, by transforming f into a variable of class numeric instead of factor.

# Copy from old column 'f' into a new variable that we name new_var
# We just keep the number.
new_var <- gsub("EF-","",f)

# as.numeric will give the underlying value of the string
tornado$new_var <- as.numeric( new_var )
head(tornado)
##     yr mo dy       date     time tz st stf stn    f new_var
## 1 2007  1  4 2007-01-04 15:45:00  3 LA  22   1 EF-1       1
## 2 2007  1  4 2007-01-04 16:35:00  3 LA  22   2 EF-1       1
## 3 2007  1  5 2007-01-05 00:27:00  3 MS  28   1 EF-1       1
## 4 2007  1  5 2007-01-05 00:40:00  3 MS  28   2 EF-0       0
## 5 2007  1  5 2007-01-05 00:57:00  3 MS  28   3 EF-1       1
## 6 2007  1  5 2007-01-05 01:07:00  3 MS  28   4 EF-1       1