# YOUR CODE HERE
lowest_nz_payment <- pfizer |>
filter(total > 0) |>
arrange(total) |>
slice_head(n = 10)In-Class Activity: Data Journalism
Data Journalism Practice
In today’s lecture, we learned how to use dplyr to filter, sort, and summarize data, as well as how to handle dates and join multiple datasets.
Now it is your turn! Use the pfizer.csv and fda.csv datasets to answer the following questions.
Question 1: Filtering and Sorting
Find the 10 doctors who received the lowest non-zero total payment (total > 0) from Pfizer. Which state are most of them from?
Question 2: Grouping and Summarizing
Group the Pfizer data by category and calculate the average (mean) payment per category. Sort the results in descending order. Which category has the highest average payment?
# YOUR CODE HERE
q2 <- pfizer |>
group_by(category) |>
summarize(mean_total = mean(total, na.rm = TRUE)) |>
arrange(desc(mean_total))Question 3: Dates and String Manipulation
In the FDA dataset, find out how many warning letters were issued in the year 2008. Hint: You can use mutate and format to extract the year from the date, just like in the lecture.
# YOUR CODE HERE
q3 <- fda |>
mutate(issued_new = as.Date(issued, format = "%m, %d, %Y"),
year = format(issued_new, "%Y")) |>
filter (year == 2008) |> nrow()Question 4: Joins
Let’s do some real data journalism! We want to find doctors who received payments from Pfizer AND received warning letters from the FDA.
Use an inner_join() to combine the pfizer and fda datasets. Match them by both first_name and last_name. How many doctors appear in both lists?
# YOUR CODE HERE