Goal: Master tidy data organization and subsetting.
- Reshape the following data so that they are tidy
r
r my_df = data_frame( group = c(,,), drug_X = c(130, 135, 140), drug_Y = c(NA, 150, 135))
For the remaining questions use the dinesafe data.
Arrange infractions from largest to smallest amount fined. Return the inspection ID, establishment name, amount fined, and infraction details.
Find the restaurant infractions with the 10 highest fines. Return the inspection ID, establishment name, amount fined, and infraction details.
Find how many (unique) inspections took place in 2017.
Looking at distinct values of the variable SEVERITY, there are two types of values labeled and “NA - Not Applicable”. Can you explain the difference between the two?
r
r dinesafe %>% distinct(SEVERITY)
# A tibble: 5 x 1
SEVERITY
<chr>
1 M - Minor
2 <NA>
3 NA - Not Applicable
4 S - Significant
5 C - Crucial
Split SEVERITY into two variables: a severity code (SEV_CODE) and a severity description (SEV_DESC): i.e. separate()
the values of SEVERITY along the hyphen (–).
Find one of my favourite spots in TO: it was inspected on 2018-03-20, got a Pass, and has the 4th smallest ID # out of all the other establishments inspected on that day. Write R code that outputs the name of the place.
(Hint: Use top_n()
with negative row index.)
Create a list of all unique Tim Hortons establishments in Toronto. The name can be either “TIM HORTONS” or “Tim Hortons”, and you should use select()
with starts_with()
to return all variables starting with “ESTABLISHMENT”.
LS0tDQp0aXRsZTogIlNUQUE1NyAtIFdvcmtTaGVldCAzIg0KZGF0ZTogIiAgIg0KYXV0aG9yOiAiTmFtZTogICAgLCBJRCM6ICAgIg0Kb3V0cHV0OiBodG1sX25vdGVib29rDQplZGl0b3Jfb3B0aW9uczogDQogIGNodW5rX291dHB1dF90eXBlOiBpbmxpbmUNCi0tLQ0KDQpgYGB7ciBzZXR1cCwgaW5jbHVkZT1GQUxTRX0NCmtuaXRyOjpvcHRzX2NodW5rJHNldChlY2hvID0gVFJVRSkNCmBgYA0KDQoqKkdvYWwqKjogTWFzdGVyIHRpZHkgZGF0YSBvcmdhbml6YXRpb24gYW5kIHN1YnNldHRpbmcuIA0KDQoxLiBSZXNoYXBlIHRoZSBmb2xsb3dpbmcgZGF0YSBzbyB0aGF0IHRo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