188k views
3 votes
You would like to sort the mtcars data frame by horsepower (the hp column). Which R statement achieves this?

A. order(mtcars, hp)
B. sort(mtcars, hp)
C. arrange(mtcars, hp)
D. arrange(mtcars, -hp)
E. None of the mentioned

1 Answer

3 votes

Final answer:

C 'arrange(mtcars, hp)'.The correct answer to sort the 'mtcars' data frame by horsepower is option C 'arrange(mtcars, hp)' using the dplyr package's arrange function.

Step-by-step explanation:

The correct answer is option C. arrange(mtcars, hp). In R, the arrange function from the dplyr package is used to sort a data frame by the values of specified columns. To sort the mtcars dataset by the horsepower column, you would use arrange with the column name (hp) as the argument.

Note that in the mtcars data frame, hp stands for horsepower, and is the target for sorting. If you need to sort in descending order, you would use a minus sign before the column name, making option D incorrect for sorting in ascending order.

The correct answer is option C. The R statement that achieves sorting the mtcars data frame by horsepower (the hp column) is arrange(mtcars, hp).

The arrange() function is a function in the dplyr package in R that allows you to reorder the rows of a data frame based on one or more variables. In this case, we want to sort the mtcars data frame by the hp column. Therefore, the correct statement is arrange(mtcars, hp).

User Cstoltze
by
8.3k points