Indexing dataframes in R

前端 未结 1 965
予麋鹿
予麋鹿 2021-01-26 18:14

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         


        
相关标签:
1条回答
  • 2021-01-26 18:21

    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.

    0 讨论(0)
提交回复
热议问题