R tutorial – How to sort lists using rstudio

You are kidding me; we can sort lists!
Yes, we have managed to bring you the groundbreaking video that will help transform your project! This video is just an introductory view of how to complete a sort on a list, but all is not what it seems.

Couple of things to note:

  • Creating a list in Rstudio means its a list that is it.
  • You need to create a vector so that that a sort of the data can give you the desired outcome you need.

So using :

#create the list
print("Example 1")
list1 <- list(5,4,3,2,1,"a","b")
print(list1)#defaults to list order
print(typeof(list1))

will get you started, but there is more to this, read on.

How we got this sorted

As you will see in the below video, we have taken an initial list and converted it to a vector using the c() function in rstudio.

Some of the functions used to sort are as follows:

  • sort
  • sort.int

The video explains the ways to use these, and some of the caveats you should watch out for as well.

 

 

Leaving you with this final thought

This video has a python equivalent, so if you want to see how we completed it there, see this blog posting How to sort Python lists

Data analytics Ireland

Python Tutorial: How to sort lists

Estimated reading time: 2 minutes

Following on from our post on how to use Python lists have you ever wondered how to sort lists for your Python project?

Our latest video on lists will go through some of the techniques available so that you can get an idea of how to structure your data and sort.

Getting to understand how to implement

In this latest video we will look at:

  • sort() method
  • sorted() function
  • sorting a list through a function

 

Adding in those extra bits to help make the process smoother

Have you thought about sorting ascending/descending?

  • There is also a discussion on this topic as well, and while an index is available for the list, which you may feel does not merit sorting, there could be other logical reasons to implement sorting.
  • Leaving out the reverse = True/False in the sorted method can have an impact, though if you require it left out of the list you have created, automatic ascending will be the default.

On this channel, we have discussed a number of different ways to manage your data. In thinking about sorting a list, why would you want to do this?

Some common reasons are:

  • To visually see if there are duplicates, either on the screen or printed out.
  • If other objects are dependant on the list, say a combo box, then having duplicates visible can help to reduce the size of their contents.
  • Iteration – If you are looking to iterate over a list, it will be quicker if it is sorted.

If you want to learn about lists, using them, and how how they can be iterated over, why not visit Data Analytics Ireland YouTube channel, there are lots of videos there that will help explain the concepts discussed here further.

To get some more links on this topic click here python sort method, it is a blog posting from our website that has some useful links and explanations for you.