Since NA is present, index 6 will be placed last because of na.last=TRUE. The largest is placed at index 7, and the other values are arranged in a decreasing manner. Here order() will sort a given list of numbers according to its index in the descending order because of decreasing=TRUE: 46. The above code gives the following output: 7 9 1 8 10 3 11 2 4 5 6 order(x,decreasing=TRUE,na.last=TRUE) order(x,decreasing=TRUE,na.last=TRUE) Since NA is present, it's index, which is 6, will be placed first because of na.last=FALSE. Here the order() will also sort the given list of numbers according to its index in the ascending order. The above code gives the following output: 6 5 4 2 11 3 10 1 8 9 7 order(x,na.last=FALSE) order(x,na.last=FALSE) Since NA is present, its index will be placed last, where 6 will be placed last because of na.last=TRUE. The above code gives the following output: 5 4 2 11 3 10 1 8 9 7 6 Let's look at an example where the datasets contain the value as symbol NA(Not available). Sorting vector using different parameters in order() The values are ordered according to the index using order() then after each value accessed using y. Here the indexing of order is done where the actual values are printed in the ascending order. The above code gives the following output: 2 4 5 6 7 9 12 Since number 2 is the smallest, which has an index as five and number 4 is index 1, and similarly, the process moves forward in the same pattern. Here the order() will sort the given numbers according to its index in the ascending order. The above code gives the following output: 5 1 7 3 4 6 2 The numbers are ordered according to its index by using order(x). Let's look at an example of order() in action.īelow the code contains variable x, which includes a vector with a list of numbers. na.last: boolean value TRUE then NA indices are put at last or FLASE THEN NA indices are put first.decreasing: boolean value TRUE then sort in descending order or FALSE then sort in ascending order.The argument above in order() states that: The syntax of order() is shown below: order(x, decreasing = TRUE or FALSE, na.last = TRUE or FLASE, method = c("auto", "shell", "quick", "radix")) Order() can sort vector, matrix, and also a dataframe can be sorted in ascending and descending order with its help, which is shown in the final section of this tutorial. R provides a different way to sort the data either in ascending or descending order Data-analysts, and Data scientists use order(), sort() and packages like dplyr to sort data depending upon the structure of the obtained data.
0 Comments
Leave a Reply. |