Ad space (banner)
๐Ÿ“ŠR Lessons
Lesson 11 / 68

Sorting a vector

This lesson covers sorting a vector with R's sort, with the aim of understanding an operation used constantly in practice.

sort() reorders a vector. Rather than changing the original in place, it returns a new sorted vector — a functional touch characteristic of R.

The sample code assigns a new, ascending-order vector to sorted_nums with sort(nums). Note that the default order is ascending.

A common early stumble is sorting in descending order. You have to pass an argument, as in sort(nums, decreasing = TRUE), and not knowing that makes descending order surprisingly awkward.

In professional work, sorting is an everyday operation in data analysis — ranking displays, ordering results.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run R directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)