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
  • 1,376
  • 2
  • 20
  • 36
  • 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
  • 153
  • 7
  • 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
  • 410
  • 1
  • 5
  • 15
  • I keep getting Error in order(-`gross to budget ratio`) : object 'gross to budget ratio' not found but I get the same error even with the other columns – Bailey Miller Mar 02 '19 at 19:32
  • 1
    try: orderedData <- data[order(data[,"gross to budget ratio"]),] # ascending order – Soren Mar 02 '19 at 19:38
  • Try `orderedData <- data[order(data$'gross to budget ratio'),]`, where you must change the `'` to backticks. You can also accept the autocomplete as you type `data$gross` and the backticks will be included. These backticks are needed because the column name has spaces – R. Schifini Mar 02 '19 at 19:41
  • I actually moved to use the arrange function but this helped me understand how to select the data. – Bailey Miller Mar 02 '19 at 20:00