【问题标题】:Item count descriptive statistics [closed]项目计数描述性统计[关闭]
【发布时间】:2015-10-08 07:27:57
【问题描述】:

我有一个数据框

x  userId  bookId  rating
1       1     412       6
2       1     454       5
3       2     412       4

等等。

基本上,一个用户给很多书评分,而一本书有很多评分。

我需要提取一些关于userId 的描述性统计信息。给出的平均评分数量,给出的平均评分等。

谁能指点我正确的方向?

【问题讨论】:

  • 看看help("aggregate")
  • 或从dplyr尝试mutate
  • 或者尝试用data.table包在SO上搜索总结...library(data.table);setDT(df)[,list(avAmount=length(unique(bookID)), avRating=mean(rating)),userID]
  • @DatamineR dplyr::mutate 只不过是 "$<-"。这就像说“尝试使用 R”

标签: r summary


【解决方案1】:

您可以使用data.table 进行这些计算:

如果您的data.frame 被称为books

require(data.table)
setDT(books)

# average rating by user
books[, mean(rating), by=userId]
#   userId  V1
#1:      1 5.5
#2:      2 4.0

# average amount of ratings given :
books[, .N, by=userId][, mean(N)]
#[1] 1.5

【讨论】:

    【解决方案2】:

    我不确定我是否得到您的确切问题/任务。但以下内容可以提供一些见解:

    data = read.table(header = T, stringsAsFactors = F,  text  = "x  userId  bookId  rating
    1       1     412       6
    2       1     454       5
    3       2     412       4")
    
    # Number of ratings per user 
    userFreq = data.frame(table(data$userId)) 
    # Var1 Freq
    # 1    1    2
    # 2    2    1
    
    # mean rating per userID
    meanRatingPerUser = aggregate(data$rating, by=list(data$userId), FUN = mean )     
    # Group.1   x
    # 1       1 5.5
    # 2       2 4.0
    
    # mean rating per book
    meanRatingPerBook = aggregate(data$rating, by=list(data$bookId), FUN = mean )   
    # Group.1 x
    # 1     412 5
    # 2     454 5
    
    
    # "Summary" function, applied per bookID
    moreStats = aggregate(data$rating, by=list(data$bookId), FUN = summary ) 
    # Group.1 x.Min. x.1st Qu. x.Median x.Mean x.3rd Qu. x.Max.
    # 1     412    4.0       4.5      5.0    5.0       5.5    6.0
    # 2     454    5.0       5.0      5.0    5.0       5.0    5.0
    

    【讨论】:

      猜你喜欢
      • 2019-06-29
      • 2016-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多