-1

Hi I have a list of values and I only need elements with highest rating i.e.5. so for below list I only need movie names which has 5 rating. I dont need movies with rating 2 and 3. How do I do this in R.

$Movie1
[1] 5

$Movie2
[1] 5

$Movie3
[1] 2

$Movie4
[1] 5

$Movie5
[1] 5

$Movie6
[1] 4

I need result as

Movie1  Movie2  Movie4   Movie6
5         5       5        5
Axeman
  • 32,068
  • 8
  • 81
  • 94
Shikhar
  • 1
  • 4
  • Something like this `lst[sapply(lst, function(x) x > 3)]`? `lst` is your `list`, and this excludes elements that are `<3`. – Maurits Evers Jan 21 '20 at 22:27
  • `v <- unlist(your_list); v[v == 5]`? – Axeman Jan 21 '20 at 22:27
  • Try `mx <- max(unlist(lst1));Filter(function(x) x == mx, lst1)` – akrun Jan 21 '20 at 22:27
  • Or `unlist(lst[lst == 5])` – markus Jan 21 '20 at 22:32
  • @markus This was really helpful . I was able to get the desired result. However when I got the max values for all column then that variable was integer and I converted that to list. I am wondering if I have to unlist it so there is no need to convert it . So how can we get the same result with integer. I have mentioned the original request in detail below, in case you need more details. – Shikhar Jan 22 '20 at 01:02

1 Answers1

0

The first step is going to be to unlist your list, then index the movie names for those that have the maximum ratings, then store them in a dataframe. Since you didn't really provide aa use case, it is hard to know exactly why you want the format you do, but here is something to get you started I hope.

my_list <- list(movie1 = 5,    # a mock movie list
                movie2 = 5,
                movie3 = 2,
                movie4 = 5,
                movie5 = 4)


new <- do.call(rbind, my_list) # unlisting the elements into a df


t(
  data.frame( 
    movie = row.names(new)[new == max(new)],   # here we store row names of max rows
    num = max(new))                            # here we provide the max rating
  )                                            # finally, transpose `t()`

Hopefully this helps. Alternatively, you could do this to get the literal output you want from a similar list:

my_list <- list(movie1 = 5,    # a mock movie list
                movie2 = 5,
                movie3 = 2,
                movie4 = 5,
                movie5 = 4)


new <- do.call(rbind, my_list) # unlisting the elements into a df

movie_names <- row.names(new)[new == max(new)]  # store all the movie names that have max rating
rep_num <- max(new)                             # store max rating (to pass to rep and eventually table
len <- length(movie_names)                      # len to pass to seq_len

table(sapply(seq_len(len), function(i){rep(movie_names[i], rep_num)})) # then finally create a function to repeat the movie name for the max rating and create a table from it. 

Brennan Beal
  • 108
  • 7
  • Thanks Brennan. So original request is below. I hope this can help you give a better answer. The dataset I have contains movie reviews given by Amazon customers. Data Dictionary: UserID – 4848 customers who provided a rating for each movie Movie 1 to Movie 206 – 206 movies for which ratings are provided by 4848 distinct users. Exploratory Data Analysis: 1) Which movies have maximum views/ratings? 2) What is the average rating for each movie? Define the top 5 movies with the maximum ratings. 3) Define the top 5 movies with the least audience – Shikhar Jan 22 '20 at 00:27
  • So I got max ratings for all columnn(movies) by applying apply function. apply(dataset,2,max,na.rm=TRUE) Now I have all columns(movies) with max ratings but we need only those movies which has max ratings. So if from apply function my answer is Movie1 -5, Movie2 -3, etc. how do I get movies with max rating. So max rating given is 5 here. – Shikhar Jan 22 '20 at 00:31
  • This link has original request :) https://stackoverflow.com/questions/59849726/how-to-find-columns-with-maximum-ratings?noredirect=1#comment105833655_59849726 – Shikhar Jan 22 '20 at 00:36
  • Hi @Shikhar, given what your second comment said, and from your original question here, does my work here not solve that problem? I'm happy to continue to help but need to know what about the above is or is not helpful. – Brennan Beal Jan 22 '20 at 01:24
  • Hey Brennan . It does help but I made it clear for you to understand if there is much easier approach to solve this. You solution is rather effective and thank you for the help. I understand now what was done and how to approach this. – Shikhar Jan 22 '20 at 01:40
  • Any comments on #3) Define the top 5 movies with the least audience. Provided lot of movies are unrated and we need to find movies(column) which are rated by maximum users (rows) – Shikhar Jan 22 '20 at 01:44
  • @Shikhar - great. If it helped, could you give an "upvote" and mark this as solved? For question 3, could you create another question with a reproducible example? If so, I'd be happy to take a look and help you out. Here is how to create a reproducible example (reprex) https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Brennan Beal Jan 22 '20 at 02:16
  • Thanks I have upvoted and will reproduce this and let you know. – Shikhar Jan 22 '20 at 04:26
  • Here is the link for second question: https://stackoverflow.com/questions/59849726/how-to-find-columns-with-maximum-ratings I have also attached image of dataset. I am looking for this question if u can help: Q) Define the top 5 movies with the least audience – Shikhar Jan 22 '20 at 22:26