0

I have an odd set of data with a weirdly named column, I want to order this data frame by that column with the highest values at the top.

Colnames:

[1] "budget"                "company"               "country"               "director"              "genre"                 "gross"                 "name"                  "rating"               


[9] "released"              "runtime"               "score"                 "star"                  "votes"                 "writer"                "year"                  "gross to budget ratio"

I want to order by the highest gross to budget ratio but I don't understand how to do that. I also am having a hard time understand the order function and how to select what I want to order by.

Bailey Miller
  • 930
  • 1
  • 13
  • 30
  • You'll need to show us some data, just do `dput(head(nameofdf))` where nameofdf is name of your data frame. – arg0naut91 Mar 02 '19 at 19:35
  • 1
    Possible duplicate of [How to sort a dataframe by multiple column(s)?](https://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-multiple-columns) – markus Mar 02 '19 at 19:40

2 Answers2

2

The error you are getting is probably from not linking the object correctly. You have to specify the vector within the dataframe in the function.

Try:

orderedData <- data[order(-data$gross to budget ratio),]

Also, I don't typically use spaces in my column names and I can't remember if it throws errors for that, so you may actually have to do with 'col name' it as:

orderedData <- data[order(-data$`gross to budget ratio`),]

Either way, if you just type data$ and then tab, you can just select your column from there and it should do it correctly.

Edit: Backticks are needed in that format, thanks Gregor.

cdtip
  • 101
  • 6
  • 3
    Definitely won't work with `$` and no backticks. Options are `data[, "gross to budget ratio"]` or `data$\`gross to budget ratio\`` to refer to the column. – Gregor Thomas Mar 02 '19 at 19:44
1

Assuming your dataframe is named data, you can use the following code.

orderedData <- data[order(gross to budget ratio),]  # ascending order
orderedData <- data[order(-gross to budget ratio),] # descending order

It will order your dataframe and stores in a new dataframe named orderedData.

Iago Carvalho
  • 370
  • 1
  • 4
  • 15