FAQ: Introduction to Data Frames in R - Arranging Rows

Community%20FAQs%20on%20Codecademy%20Exercises

This community-built FAQ covers the “Arranging Rows” exercise from the lesson “Introduction to Data Frames in R”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn R

FAQs on the exercise Arranging Rows

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

1 Like

So this arranges the data in a new dataframe… how do I arrange the data in the original dataframe?

Hi,

I use arrange(desc) for both the tasks in this, but the first one asks for ascending order, not descending. What is the code for ascending order?

Hello, @elisea2967290632. Welcome to the forum!

Ascending:

sorted_df_ascending <- arrange(df, column_name)
#or using pipe
sorted_df_ascending <- df %>% arrange(column_name) 

Descending:

sorted_df_descending <- arrange(df, desc(column_name))
#or using pipe
sorted_df_descending <- df %>% arrange(desc(column_name))
2 Likes
artists <- arrange(artists, group)

To arrange data in the original dataframe, you need to update the dataframe. Is this what you are asking about?

1 Like

In all the lessons after the introduction to pipping I have not been able to pipe in an argument. In this example I wanted to write:

artists %>%
arrange(group)
group_asc <- arrange(group)
group_asc

to pipe in artists into the arrange function. But I am received with: Error in arrange(group) : object ‘group’ not found

I end up just giving up and not doing the pipping so my result looks like:
arrange(artists, group)
group_asc <- arrange(artists, group)
group_asc

This will allow me to move on, but I am not sure why the pipping isn’t working. I would like to know how because in all the lessons they use it as examples.

Let’s first look at the code snipped which worked for you:

arrange(artists, group)
group_asc <- arrange(artists, group)
group_asc

In this snippet, the first statement [arrange(artists, group)] by itself doesn’t help us in completing our task. The arrange function returns a new data frame (it doesn’t change the existing data frame artists). So the first statement [arrange(artists, group)] takes the data frame artists, sorts it by the group column in ascending order and then throws away the result without saving this resulting sorted data frame. If you delete this first line and write the code as

group_asc <- arrange(artists, group)
group_asc

you will see that this works, because in the statement [group_asc <- arrange(artists, group)], you are assigning the returned sorted data frame to the variable group_asc.


Now let’s look at how you attempted to do the same sorting via piping.

artists %>% arrange(group)
group_asc <- arrange(group)
group_asc

Again the first statement [artists %>% arrange(group)] is useless by itself and should be deleted. This first statement does correctly pipe the artists data frame to the arrange function BUT like before, the resulting sorted data frame is just thrown away without being saved. The second statement generates the error you see [ Error in arrange(group) : object ‘group’ not found]. In this statement i.e. [group_asc <- arrange(group)], you call the arrange function without any data frame but just a column name. The compiler thinks group is the data frame and when it sees that there is no data frame called group, it raises the error.


The correct way to do the piping and then view the result would be:

group_asc <- artists %>% arrange(group)
group_asc

The first statement takes the data frame artists and pipes it into the arrange function [this is equivalent to arrange(artists, group)], and then saves the resulting new sorted data frame to the variable group_asc.

1 Like

There is one thing that I don’t get: every time I make a new line I need to pipe it again, otherwise the new line will refer to the first pipe, right?
Why I don’t need to pipe the arrange line as well?

artists<-artists%>%
  select(-country,-year_founded,-albums) **%>%** 
    filter(spotify_monthly_listeners>20000000,genre!='Hip Hop')**%>%**
      arrange(desc(youtube_subscribers))
  head(artists)

I don’t quite follow your question. Perhaps you could elaborate a bit more.

artists <- artists %>%
  select(-country, -year_founded, -albums) %>%
  filter(spotify_monthly_listeners > 20000000, genre != 'Hip Hop') %>%
  arrange(desc(youtube_subscribers))
head(artists)

In this snippet, we

  1. we take the data frame artists and pipe it to the select function. The country, year_founded and albums columns are excluded and a NEW data frame is returned by the select function. We haven’t done anything with this returned data frame yet. Neither have we assigned it to a variable yet NOR have we thrown it away yet.

  2. The new data frame from the step 1 is now piped into the filter function and after filtering the rows according to our two conditions, another NEW data frame is returned. Again, this new data frame hasn’t been assigned or discarded yet.

  3. Now, we take the data frame from step 2 and pipe it into the arrange function. The arrange function sorts the data frame in descending order of youtube_subscribers and returns a NEW data frame.

  4. Finally, we assign the data frame from step 3 to the variable artists thereby overwriting our original artists data frame.

EDIT: Just to be clear, the piping symbol %>% goes between the data frame and the function.

dataFrame %>% function(...other arguments...)

Alternatively, just for formatting you could also write it as

dataFrame %>% 
  function(...other arguments...)

These above two lines are both equivalent to

function(dataFrame, ...other arguments...)

So, in the snippet we are discussing

artists <- artists %>% // Piping symbol of doing step 1
  select(-country, -year_founded, -albums) %>% // Piping symbol of doing step 2
  filter(spotify_monthly_listeners > 20000000, genre != 'Hip Hop') %>% // Piping symbol of doing step 3 
  arrange(desc(youtube_subscribers))
head(artists)

Now I got it.

So basically need to pipe (%>%) every new function (line) if I want it to be used in the following line. The last line, if not piped, will finish the cycle.

Thanks for helping