2015-01-15 63 views
0

我有一個包含30列的文件。這些包括用戶ID,條目ID,電影名稱,評級,日期,其餘的是對電影所屬的類型進行分類。流派類別是行中具有二進制值的列名稱。如果電影屬於流派,則它在適當的列下有1,否則爲0。我想計算每種流派的平均評分,並想知道是否有更短的流程可用?如何計算每個類型的平均評分

我目前已經嘗試過濾數據,選擇每個類型的值是'1',然後計算平均評級。但我有近24種流派,以這種方式做我的想法效率不高。我嘗試過的另一種方式是遍歷流派列,並再次過濾每個類型,其中值爲'1'但循環消耗大量時間,並且當數據設置很大(超過100K行)時,R有時可以播放我注意到了。

我想問一下,是否有另一種避免像融化,dcast或其他方法可以完成相同工作的循環的方式?

我正在提供我的數據集的dput

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)) 
+0

你可以發佈'dput(yourdata)'的結果嗎?由於選項卡的原因,從這種格式讀入R是非常困難的。 – 2015-01-15 18:40:46

+0

由於我不熟悉dput的機制,我從原來的第10行開始。 – syebill 2015-01-15 18:51:02

+0

你可以簡單地做'dput(head(yourdata,10))'。這會讓某人將其粘貼到R控制檯並獲得相同的對象。事實上,您的文件需要大量的讀取才能讀入R(因爲製表符不會被保留) – 2015-01-15 18:51:55

回答

3

這是一個很好的使用情況dplyrtidyr

library(dplyr) 
library(tidyr) 
dat %>% gather(genre, value, unknown:Family) %>% filter(value == 1) %>% 
    group_by(genre) %>% summarize(average = mean(Rating)) 

此代碼:

  • 收集各電影/流派對到一個單獨的行(會有每個電影有多行)
  • 僅針對電影屬於某個類型的情況的過濾器
  • 組按流派,並在每一個總結髮現平均分數(你可以執行像中值或標準差,以及其他操作)
+0

你們都很棒 – syebill 2015-01-15 19:13:03

+0

@ user42571。別客氣。另外請注意,一旦你在'tridied <- dat %>%gather(風格,值,未知:系列)%​​>%過濾器(值== 1)'格式中獲得它,你就可以製作很酷的圖。例如,試試'library(ggplot2); ggplot(tidied,aes(流派,評分))+ geom_boxplot()'。 – 2015-01-15 19:15:09

+0

我想問問包「tidyr」不適用於3.0.3版。我是否需要以前的版本才能使用它?我正在運行RStudio-版本0.98.1091 – syebill 2015-01-15 19:24:10

-1

舊時尚的方式也適用:

genres <- c('Action','Adventure','Animation') 
means <- numeric(length(genres)) 
names(means) <- genres 
for(g in genres) 
    meanRatings[g] <- mean(myData$Rating[mydata[,g]==1]) 
means 
+0

我覺得不應該在R中建議運行循環來完成這些任務。 – CMichael 2015-01-23 20:52:05