Page 7 of 54
1 <NA> 2010 1 0 2 14 22 11 50
6 SA 2010 0 0 0 2 6 2 10
11 EU 2010 0 0 1 3 7 4 15
16 PA 2010 0 0 1 2 2 0 5
Things to note include the syntax for the data filename and variable separated by a $, e.g.
DealerSatisfaction and Year as DealerSatisfaction$Year. To designate the test for “equals” use a double
==. You can use this syntax to find all the data required for this step. One of the really nice things
about RStudio is that you can move between commands you have used by the “up” and “down” arrows
on your keyboard. So, to go to the next year you only need to use the up arrow twice to go to the
command to subset the data and then change the year to 2011 to get the answers for the next year, and so
on. When you get to the part that asks for this data for End-User Satisfaction you just need to use the up
arrow appropriately and change the file name.
DO NOT forget to change the data object name for each command you use to store your results in. If
you do not change the data object name you will be continuously writing over your previous results.
To find the sum by year and return the value use the information you got before and sum, e.g.:
> y2010L0 <- sum(y2010[,3])
> y2010L0
[1] 1
That is, the sum for the year 2010 for all regions is 1. You can do this for all the instances you need to.
Choose data object names that make sense so that as you need them you can easily find them again and
again.
Keep in mind that R uses typical matrix notation, i.e. [rows, columns]. So you can always find the value
in an element in a matrix by its [row, column] designation.
Step 2
Find the number of failures in the Mower Test.
When the author completed this in Excel he just used the “COUNTIF” function for each column of his
spreadsheet Mower Test. This will take a bit more syntax in R but is relatively easy using the sapply()
function. There is actually a whole family of “apply” functions; e.g. apply, sapply, lapply, etc. All are
helpful in their way so check them out, e.g. in the answer at
http://stackoverflow.com/questions/3505701/r-grouping-functions-sapply-vs-lapply-vs-apply-vs-tapply-
vs-by-vs-aggrega. For our question, we’ll use sapply() and the data file MowerTest but exclude the first
column which is the variable “Observations”. We’ll return the values of “Pass” and “Fail” as a table for
each of the Samples 1 through 30. The syntax we’ll use is: