sort an array in r

In R, you can sort an array using the sort() function. By default, the function sorts the array in ascending order.

Here is an example code to sort an array in R:

main.r
# Create an array
arr <- array(c(3, 1, 4, 1, 5, 9, 2, 6, 5), dim = c(3, 3))

# Sort the array
sorted_arr <- sort(arr)

# Print the sorted array
sorted_arr
155 chars
9 lines

Output:

main.r
[1] 1 1 2 3 4 5 5 6 9
22 chars
2 lines

In this example, the array arr is created using the array() function with a vector of elements and dimensions of 3 rows and 3 columns. The sort() function is used to sort the array in ascending order, and the resulting sorted array is stored in sorted_arr. Finally, the sorted array is printed to the console.

related categories

gistlibby LogSnag