69.3k views
4 votes
Consider the vector x: x <- c(2, 43, 27, 96, 18) Match the following outputs to the function which produces that output. Options include sort(x), order(x), rank(x) and none of these

User Nitz
by
6.2k points

1 Answer

3 votes

Completed Question

Outputs to be matched to the functions are:

  • 1,2,3,4,5
  • 1,5,3,2,4
  • 1, 4, 3, 5, 2
  • 2, 18, 27, 43, 96

Answer:

  • sort(x): 2, 18, 27, 43, 96
  • order(x): 1, 5, 3, 2, 4
  • rank(x) : 1, 4, 3, 5, 2
  • none of these : 1, 2, 3, 4, 5

Explanation:

Given the vector x: x <- c(2, 43, 27, 96, 18)

Sort

In R, the sort(x) function is used to arrange the entries in ascending or descending order. By default, R will sort the vector in ascending order.

Therefore, the output that matches the sort function is:

sort(x): 2, 18, 27, 43, 96

Rank

The rank function returns a vector with the "rank" of each value.

x <- c(2, 43, 27, 96, 18)

  • 2 has a rank of 1
  • 43 has a rank of 4
  • 27 has a rank of 3
  • 96 has a rank of 5
  • 18 has a rank of 2

Therefore, the output of rank(x) is: 1, 4, 3, 5, 2

Order

When the function is sorted, the order function gives the previous location of each of the element of the vector.

Using the sort(x) function, we obtain: 2, 18, 27, 43, 96

In the vector: x <- c(2, 43, 27, 96, 18)

  • 2 was in the 1st position
  • 18 was in the 5th position
  • 27 was in the 3rd position
  • 43 was in the 2nd position
  • 96 was in the 4th position

Therefore, the output of order(x) is: 1, 5, 3, 2, 4

User Dave Snyder
by
6.7k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.