Home > Enterprise >  Find the title of the show or movie with the shortest run-time in R
Find the title of the show or movie with the shortest run-time in R

Time:08-25

Hi guys how can I find the title of the show or movie with the shortest runtime in a given dataset. I have tried min(titles$runtime) but that only returns the minimum runtime not the title(which is inside the title heading) of the movie or show which are part of part of the "types" heading

CodePudding user response:

You can use that code

titles[which.min(titles$runtime),]

To find out which row has the shortest run time.

From your code I assume that titles is the name of your dataframe. It is always dataframe and then columnname

df[which.min(df$colname),]

for more explanation see this website https://www.geeksforgeeks.org/how-to-extract-the-dataframe-row-with-min-or-max-values-in-r/

CodePudding user response:

A solution based on toy data:

Data:

df <- data.frame(
  title = c("X", "Y", "Z"),
  runtime = c(12.5,13.7,10)
)

Solution in base R to obtain just the title value:

df$title[df$runtime == min(df$runtime)]
[1] "Z"

To obtain the row with the value in question:

df[df$runtime == min(df$runtime),]
  title runtime
3     Z      10

Solution in tidyverse to get the entire row:

library(tidyverse)
df %>%
  filter(runtime == min(runtime))

To get just the title value:

df %>%
  filter(runtime == min(runtime)) %>%
  pull(title)
  •  Tags:  
  • r
  • Related