Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
order()
r

What is the order() function in R?

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

The order() function in R is used to return a permutation that simply orders or rearranges a sequence of numeric, complex, character, or logical vectors in ascending or descending order by their index positions.

Syntax

order(…, na.last = TRUE, decreasing = FALSE)
Syntax for the order() function in R

Parameters

The order() function takes the following parameter values:

  • ...: This is a sequence of numeric, complex, character, or logical vectors of the same length to be ordered in ascending or descending order. This is a required parameter.
  • na.last: This takes a boolean value (TRUE or FALSE). If TRUE, the missing value in the input data is put last. If FALSE, they are removed first and NA values are also removed. This is an optional parameter.
  • decreasing: This takes a boolean value indicating if the sort order should be in ascending (increasing) or descending (decreasing) order. This is an optional parameter.

Example

# creating a numerical object
a <- c(100, 5, 2, 8, 10, 1, 0.5)
# sorting increasingly
order(a, decreasing=TRUE)
# sorting decreasignly
order(a, decreasing = FALSE)
Implementing the order() function

Explanation

  • Line 2: We create a numerical object, a.
  • Line 5: We sort the values of the object in ascending order by their index position using the order() function.
  • Line 8: We sort the values of the object in descending order by their index position using the order() function.

RELATED TAGS

communitycreator
order()
r

CONTRIBUTOR

Onyejiaku Theophilus Chidalu

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring