Chapter 2 Question 11
This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
The following table lists a portion of the average writing and math SAT sores for the 50 states as well as the District of Columbia, Puerto Rico, and the U.S. Virgin Islands for the year 2017 as reported by the College Board.
#Step 1) Load the data into myData data frame
library(readxl)
myData <- read_excel("Ch2_Q11_Data_File (1).xlsx")
myData
- Sort the data by writing scores in descending order. Which state has the highest average writing score? What is the average math score of that state?
sortedData = myData[order(myData$Writing, decreasing = TRUE),]
state = sortedData[1,1]
wrscore = sortedData[1, 2]
mtscore = sortedData[1, 3]
sprintf("%s has the highest writing score of %s and math score of %s", state, wrscore, mtscore)
[1] "Minnesota has the highest writing score of 643 and math score of 655"
- Sort the data by math scores in ascending order. Which state has the lowest average math score? What is the average writing score of that state?
sortedData = myData[order(myData$Math),]
state = sortedData[1,1]
wrscore = sortedData[1, 2]
mtscore = sortedData[1, 3]
sprintf("%s has the lowest math score of %s and writing score of %s", state, mtscore, wrscore)
[1] "Virgin Islands has the lowest math score of 445 and writing score of 490"
- How many states reported an average math score higher than 600?
nstate = length(which(myData$Math > 600))
sprintf("%s states have math scores greater than 600.", nstate)
[1] "13 states have math scores greater than 600."
- How many states reported an average writing score lower than 550?
nstate = length(which(myData$Writing < 550))
sprintf("%s states have writing scores less than 550.", nstate)
[1] "25 states have writing scores less than 550."
LS0tCnRpdGxlOiAiUiBOb3RlYm9vayIKb3V0cHV0OiBodG1sX25vdGVib29rCi0tLQojIENoYXB0ZXIgMiBRdWVzdGlvbiAxMQoKVGhpcyBpcyBhbiBbUiBNYXJrZG93bl0oaHR0cDovL3JtYXJrZG93bi5yc3R1ZGlvLmNvbSkgTm90ZWJvb2suIFdoZW4geW91IGV4ZWN1dGUgY29kZSB3aXRoaW4gdGhlIG5vdGVib29rLCB0aGUgcmVzdWx0cyBhcHBlYXIgYmVuZWF0aCB0aGUgY29kZS4gCgpUcnkgZXh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