Sorting Results


  • simple sort by one column
companyOrder <- order(companiesData$margin)
  • reorder rows in data frame
companiesOrdered <- companiesData[companyOrder,]

a comma followed by nothing defaults to "all columns"

companiesOrdered <- companiesData[order(companiesData$margin),]

specify just certain columns

companiesOrdered <- companiesData[order(companiesData$margin),c("fy", "company")]

sort in descending order

companyOrder <- order(-companiesData$margin)

companiesOrdered <- companiesData[companyOrder,]

companiesOrdered <- companiesData[order(-companiesData$margin),

  • sort one column ascending and another column descending
companiesData[order(companiesData$fy, -companiesData$margin),]

companiesOrdered <- companiesData[with(companiesData, order(fy, -margin)),]
  • doBy::orderBy()
    • syntax
      • The ~ at the beginning
        • "by" (as in "order by this").
      • If you want to order by descending
        • put a minus sign after the tilde and before the column name
orderBy(~columnName + secondColumnName, data=dataFrameName)
companiesOrdered <- orderBy(~-margin, companiesData)
  • plyr::arrange(), dplyr::arrange()
    • the syntax
arrange(dataFrameName, columnName, secondColumnName)

# To sort descending, use desc(columnName))
companiesOrdered <- arrange(companiesData, desc(margin))

Reference:

results matching ""

    No results matching ""