good day I don´t understand a topic here, is like it works but I can´t understand why I have this database
# planets_df is pre-loaded in your workspa
I believe that I have created an example that matches your description. For the mtcars
data set, which is pre-loaded in any R session, we can sort based on the variable mpg
.
The function order
returns the row indices sorted by mpg
in this case. The ordering
variable indicates the order that the rows should be presented in by storing the row indices based on mpg
.
ordering <- order(mtcars$mpg)
This next step indicates that we want the rows of mtcars
as specified by ordering
. Essentially ordering
is the order of the rows we want and so we pass that object to the row portion the call to mtcars
.
mtcars[ordering,]
If we instead passed ordering
as the columns, we would be reordering the columns of mtcars
instead of the rows.