R Guided Project: Covid 19 Part 6 problems

I’m doing part 6 of this guided project and I’ve created the vectors and named them and that seems to work okay.

countries <- covid_top_10$Country_Region
tested_cases <- covid_top_10$tested
positive_cases <- covid_top_10$positive
active_cases <- covid_top_10$active
hospitalized_cases <- covid_top_10$hospitalized
names(tested_cases) <- countries
names(positive_cases) <- countries
names(active_cases) <-  countries
names(hospitalized_cases) <- countries

The next question in the section has confused me.
3. Identify the top three positive against tested cases.

Divide the vector positive_cases by the vector tested_cases using the operator /.
Identify the top three ratio.
Store the result as the named vector, positive_tested_top_3, where each country name is associated with its ratio.

I’ve had to create an interim vector before sorting and isolating the head(3).

  sort(decreasing = TRUE) %>%
  head(3)

positive_tested_top_3

This doesn’t seem very elegant but does seem to work. Is there a better way? thanks in advance.

1 Like

Hello @bruce_burnett,

What you did is interesting and elegant! Since we are working with vectors in the part6, you can simply do this, without using pipe (%>%)

vect <- positive_cases / tested_cases
sorted_vect <- sort(vect, decreasing = T)
positive_tested_top_3 <- head(sorted_vect, 3) # equivalent to sorted_vect[1:3]

However, since we didn’t teach the function sort() in that course, we aim at something simpler (this would also help you practice vectors) in three steps:

  1. Make the operation positive_cases/tested_cases (R will display the result right after).
  2. Check the result and identify the top 3 countries.
  3. Create manually a named vector that will contain those top 3 countries with their names.

Best,
John.

2 Likes

Thanks John!

BW

Bruce

1 Like