Filtering and Arranging data

I am hoping someone could help please? I am currently doing a course in R and am having trouble with the below exercise. I am brand new to R so know this will be very simple for the majority of you!

I have pasted below what I have typed in in **, but I must be missing something and/or have something wrong, as whenever I go to click to open the spreadsheet, it just looks exactly the same? Can you advise where I am going wrong please?

To access this package, simply install and load gapminder . You can install gapminder, either by clicking on the Packages tab and navigating to the Install shortcut, or by running install.packages("gapminder") in the console. – managed to do this

As usual, we'll also need the tidyverse:
library(gapminder) – managed to do this
library(tidyverse) – managed to do this
Can you create an ordered subset of the gapminder data for the year 2007?-

First:
• create a new object called gap_2007

gap_2007 <- gapminder, typed into the script window

Then combine two functions from dplyr to:
• filter() the gapminder data to extract only observations from the year 2007
gap_2007 %>%
** filter(year == 2007) %>%**
• arrange() the data to sort it in descending order of life expectancy (lifeExp ).
gap_2007 %>%
** arrange(desc == lifeExp) %>%**

# print out the result

Thank you so much in advance,

you may visit the following website

And look at the examples, you will find all you need.

example.4 shows how to arrange data in descending order.

Since it is an exercise, I don't want to share the solution :slight_smile: :innocent:

In R any results you calculate are ephemeral if you dont assign them to a name that would allow you to refer to them later. The assignment operator <- is used for this.

Create a new script and paste this code

library(tidyverse)
library(gapminder)

#   • create a new object called gap_2007
#   • gap_2007 <- gapminder, typed into the script window

gap_2007 <- gapminder

# Then combine two functions from dplyr to:
#   • filter() the gapminder data to extract only observations from the year 2007
# gap_2007 %>%
#   ** filter(year == 2007) %>%**
#   • arrange() the data to sort it in descending order of life expectancy (lifeExp ).
# gap_2007 %>%
#   ** arrange(desc == lifeExp) %>%**

gap_2007 %>%
  filter(year == 2007) %>% 
  arrange(desc(lifeExp))

The result will be

This way I can see the result in the output. However, I have not assigned the result to any variable.
For these reason if you type gap_2007 you will see that it does not only include 2007 and that the data is not sorted.
typing gap_2007 this is the result

To make the changes permanent you must assign the result to a variable (it can be a new one or overwrite the existing one):

gap_2007 <- gap_2007 %>%
  filter(year == 2007) %>% 
  arrange(desc(lifeExp))

typing gap_2007 this is the result

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.