title: How do you calculate the average rating per genre
body: <p>I have a file with 30 columns. These include userid, itemid, moviename, rating, date and the rest are to classify genres a movie belongs to. The genre categories are column names with binary values in the rows. If a movie belongs to a genre, it has a 1 under the appropriate column and 0 otherwise. I want to calculate the average rating per genre and want to know if there is a shorter process available?</p>
  
  <p>I have currently tried filtering the data by selecting each genre where the value is '1' and then calculating the average rating. But I have almost 24 genres and doing it in this way is inefficient i think. Another way I have tried is to loop through the genre columns and again filtering each genre where value is '1' but loops consume alot of time and when the data is set is large(more than 100K rows), R can play up sometimes as I have noticed. </p>
  
  <p>I want to ask if there is another way which avoids a loop like melt,dcast or another method that can get the same job done?</p>
  
  <p>I am providing the <code>dput</code> of my dataset.</p>
  
  <pre><code>dput(data)
  structure(list(user_id = c(10L, 890L, 867L, 5L, 320L, 630L, 151L, 
  699L, 21L, 450L, 179L, 135L, 314L, 487L, 735L, 823L, 169L, 889L, 
  846L), item_id = c(447L, 660L, 191L, 441L, 1052L, 568L, 414L, 
  1061L, 872L, 33L, 302L, 581L, 568L, 280L, 181L, 503L, 498L, 207L, 
  497L), Movie_title = structure(c(6L, 11L, 2L, 3L, 9L, 17L, 15L, 
  10L, 14L, 8L, 13L, 12L, 17L, 18L, 16L, 5L, 1L, 7L, 4L), .Label = c("African Queen, The (1951)", 
  "Amadeus (1984)", "Amityville Horror, The (1979)", "Bringing Up Baby (1938)", 
  "Candidate, The (1972)", "Carrie (1976)", "Cyrano de Bergerac (1990)", 
  "Desperado (1995)", "Dracula: Dead and Loving It (1995)", "Evening Star, The (1996)", 
  "Fried Green Tomatoes (1991)", "Kalifornia (1993)", "L.A. Confidential (1997)", 
  "Love Jones (1997)", "My Favorite Year (1982)", "Return of the Jedi (1983)", 
  "Speed (1994)", "Up Close and Personal (1996)"), class = "factor"), 
      Rating = c(4L, 2L, 5L, 1L, 2L, 4L, 5L, 3L, 2L, 5L, 4L, 4L, 
      5L, 5L, 4L, 5L, 3L, 3L, 5L), Date = structure(c(7L, 15L, 
      12L, 4L, 1L, 2L, 9L, 8L, 19L, 14L, 18L, 10L, 6L, 16L, 5L, 
      11L, 17L, 13L, 3L), .Label = c("1/14/1998", "1/25/1998", 
      "1/5/1998", "10/1/1997", "10/13/1997", "10/26/1997", "10/27/1997", 
      "11/10/1997", "11/15/1997", "11/18/1997", "11/2/1997", "11/21/1997", 
      "11/22/1997", "12/18/1997", "12/24/1997", "12/30/1997", "3/31/1998", 
      "4/10/1998", "9/22/1997"), class = "factor"), unknown = c(0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L), Action = c(0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 
      1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L), Adventure = c(0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 
      1L, 0L, 0L), Animation = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Children = c(0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L), Comedy = c(0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), Crime = c(0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L), Documentary = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Drama = c(0L, 
      1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 
      0L, 1L, 0L), Fantasy = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Film.Noir = c(0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L), Horror = c(1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Musical = c(0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L), Mystery = c(0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Romance = c(0L, 
      0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 0L, 
      1L, 1L, 0L), Sci.Fi = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L), Thriller = c(0L, 
      0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 
      0L, 0L, 0L), War = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L), Western = c(0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L), Short = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), History = c(0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L), Biography = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Sport = c(0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L), Family = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L)), .Names = c("user_id", 
  "item_id", "Movie_title", "Rating", "Date", "unknown", "Action", 
  "Adventure", "Animation", "Children", "Comedy", "Crime", "Documentary", 
  "Drama", "Fantasy", "Film.Noir", "Horror", "Musical", "Mystery", 
  "Romance", "Sci.Fi", "Thriller", "War", "Western", "Short", "History", 
  "Biography", "Sport", "Family"), class = "data.frame", row.names = c(NA, 
  -19L))
  </code></pre>