0

I have a dataframe named data_devoir_grp with movies statistics and I have to find the main actor of the most expensive movie. The output must be the name of the main actor and also the budget amount of the movie. In my dataframe, there is a column 'budget' showing the cost of each movie and a column 'actor_1_name' showing the main actor's name of each movie. This is what I already tried in order to know the most expensive movie :

  • aggregate(data_devoir_grp, by = list(data_devoir_grp$budget), FUN = max)

  • summary(budget,data = data_devoir_grp, fun = max(x))

  • max(data_devoir_grp$budget)

But none of this is working. Do you have any idea ?

Thank you !

  • [order the data.frame](https://stackoverflow.com/a/1296745/5977215) by the budget (descending) and select the first row. – SymbolixAU Nov 25 '19 at 23:46
  • `None of this is working` is a bit unspecific. What is not working with `max(data_devoir_grp$budget)`? – coffeinjunky Nov 25 '19 at 23:46
  • Yes sorry, the output is Error in Summary.factor(c(147L, 266L, 150L, 155L, 440L, 198L, 153L, 158L, : ‘max’ not meaningful for factors – jordandupuy Nov 26 '19 at 00:04
  • Convert your factor to a numeric with `as.numeric(as.character(...))`, then `max` will work. – coffeinjunky Nov 26 '19 at 01:02

2 Answers2

1

Here is a way using the dplyr library. You need to make sure the budget field of your dataframe is numeric first though.

 data_devoir_grp$budget <- as.numeric(levels(data_devoir_grp$budget))[data_devoir_grp$budget]


 library(dplyr)
 tmp <- data_devoir_grp %>%
      filter(budget == max(budget)) %>%
      select(actor_1_name, budget)
jalind
  • 491
  • 1
  • 5
  • 11
  • okay in fact my problem is that budget is not numeric but factor. Is there a way to transform the variable into numeric without losing my data ? – jordandupuy Nov 26 '19 at 00:28
  • Yes. Run the first line of the code I provided above. That should transform data_devoir_grp$budget into numeric without loss of information. – jalind Nov 26 '19 at 00:30
  • See here for more info on converting factors to numeric: https://stackoverflow.com/questions/3418128/how-to-convert-a-factor-to-integer-numeric-without-loss-of-information – jalind Nov 26 '19 at 00:41
1

Have you try:

data_devoir_grp[data_devoir_grp$budget == max(data_devoir_grp$budget),'actor_1_name']

dc37
  • 15,840
  • 4
  • 15
  • 32
  • it gives me the same output : Error in Summary.factor(c(147L, 266L, 150L, 155L, 440L, 198L, 153L, 158L, : ‘max’ not meaningful for factors – jordandupuy Nov 26 '19 at 00:05
  • Can you edit your question to provide the output of `str(data_devoir_grp)` and a small [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of your data. It will makes things easier for people trying to assist you. – dc37 Nov 26 '19 at 00:15