Wednesday, 4 October 2023

Slip2-BigData-Write an R program to sort a Vector in ascending and descending order.

 Slip2

Write an R program to sort a Vector in ascending and descending order.

x = c(1,4,8,9,2,6)

print("Original Vectors:")

print(x)

print("Sort in ascending order:")

print(sort(x))

print("Sort in descending order:")

print(sort(x, decreasing=TRUE))

 

Output

> x = c(1,4,8,9,2,6)

> print("Original Vectors:")

[1] "Original Vectors:"

> print(x)

[1] 1 4 8 9 2 6

> print("Sort in ascending order:")

[1] "Sort in ascending order:"

> print(sort(x))

[1] 1 2 4 6 8 9

> print("Sort in descending order:")

[1] "Sort in descending order:"

> print(sort(x, decreasing=TRUE))

[1] 9 8 6 4 2 1