# Select rows based on conditions of different columns in R

I have a csv file “bollywood.csv” bollywood.csv (3.5 KB)

Problem statement :

# # Write a command to find the names of the movies which have the maximum Ocollection, Wcollection, Fwcollecion & Tcollection
#you should get 4 names .Store them in a data frame and show on which category they have max value

Could someone please help on getting desired output .I am new to R ,would be happy on getting help on this .

Desired output :

movie name collection
“Sultan” “Ocollection”
“Dangal” “Tcollection”
“Sultan” “Fwcollection”
“Sultan” “Wcollection”

My code :
library(“dplyr”)

bollywod = read.csv(“bollywood.csv” ,stringsAsFactors = TRUE)
View(bollywod)

OC=max(bollywod\$Ocollection)
WC=max(bollywod\$Wcollection)
FW=max(bollywod\$Fwcollection ,na.rm = TRUE)
TC=max(bollywod\$Tcollection)

bollywod_result= bollywod %>% filter(Ocollection == OC |Tcollection ==TC | Wcollection ==WC | Fwcollection==FW) %>% select(Movie)

library(reshape)

#transposed dataset to make the comparison easy of max collections within group
bolly_melt <- melt(bollywood,id=c(“Lead”, “Movie”, “Rdate”, “Verdict”))

bolly_cast <-
bolly_melt %>%
group_by(variable) %>%
filter(value==max(value,na.rm=T)) %>%
select(Movie,variable)

This is another approach with data.table.

library(data.table)
Movie Lead Rdate Ocollection Wcollection Fwcollection Tcollection Verdict
1: Dangal Aamir 23-Dec-16 29.78 106.95 197.53 386.68 Super Hit
2: Wajah Tum Ho Sharman 16-Dec-16 2.86 7.79 10.29 10.29 Flop
3: Befikre Ranveer 9-Dec-16 10.36 34.36 48.75 59.30 Average
4: Kahaani 2 Vidya 2-Dec-16 4.25 16.97 24.26 30.56 Average
5: Dear Zindagi Shahrukh 25-Nov-16 8.75 32.50 47.00 64.44 Hit
6: Force 2 John 18-Nov-16 6.05 20.05 30.15 30.15 Average

datIn_m ← melt(datIn, id.vars=“Verdict”, measure.vars = 4:7)
Verdict variable value
1: Super Hit Ocollection 29.78
2: Flop Ocollection 2.86
3: Average Ocollection 10.36
4: Average Ocollection 4.25
5: Hit Ocollection 8.75
6: Average Ocollection 6.05

result ← datIn_m[ , .(Maxvar = max(value, na.rm = TRUE)), by=variable]
result
variable Maxvar
1: Ocollection 36.54
2: Wcollection 180.36
3: Fwcollection 229.16
4: Tcollection 386.68