2016-11-06 30 views
0

我有这样的数据集R中使用dplyr:如何与不同的标准总结在同一列的数据

user_id     business_id    date  stars review_length pos_words neg_words net_sentiment 
Xqd0DzHaiyRqVH3WRG7hzg vcNAWiLM4dR7D2nwwJ7nCA 17/05/07 5 94    4  1    3 
H1kH6QZV7Le4zqTRNxoZow vcNAWiLM4dR7D2nwwJ7nCA 22/03/10 2 114    3  7    -4 
zvJCcrpm2yOZrxKffwGQLA vcNAWiLM4dR7D2nwwJ7nCA 14/02/12 4 55    6  0    6 
KBLW4wJA_fwoWmMhiHRVOA vcNAWiLM4dR7D2nwwJ7nCA 2/03/12  4 97    0  3    -3 
zvJCcrpm2yOZrxKffwGQLA vcNAWiLM4dR7D2nwwJ7nCA 15/05/12 4 53    1  2    -1 


yelp<- read.csv("yelp_ratings.csv") 
colnames(yelp) 
[1] "user_id"  "business_id" "date"   "stars"   "review_length" 
[6] "pos_words" "neg_words"  "net_sentiment" 

我需要使用dplyr确定有最好的和最差的评级业务---确定通过net_sentiment中的值---并确定给出该特定业务ID的最佳和最差评级(使用net_sentiment中的值)的用户。

继承人什么,我现在所拥有的,

yelp %>% 
    group_by(business_id,user_id) %>% 
    summarise(net_sentiment = max(net_sentiment)) %>% 
    arrange(desc(net_sentiment)) %>% 
    head(n=20) 

这使打印出来的,从我的数据集

   business_id    user_id net_sentiment 
1 -5RN56jH78MV2oquLV_G8g xNb8pFe99ENj8BeMsCBPcQ   80 
2 gVYju3XRcO1R4aNk7SZJcA xNb8pFe99ENj8BeMsCBPcQ   78 
3 ORiLSAAV4srZ_twFy1tWpw xNb8pFe99ENj8BeMsCBPcQ   77 
4 gVYju3XRcO1R4aNk7SZJcA ULOPLvLghKZrfo3PhwbPAQ   74 
5 4uGHPY-OpJN08CabtTAvNg xNb8pFe99ENj8BeMsCBPcQ   72 

这说明企业最高net_sentiment得分,也是用户谁给出了net_sentiment分数。

我打算实现的是类似

对于商务用最好的评价:

  business_id user_id_best_rating pos_net_sentiment user_id_worst_rating neg_net_sentiment 
-5RN56jH78MV2oquLV_G8g xNb8pFe99ENj8BeMsCBPcQ    80    user123    -50 

对于业务最坏的评价:

business_id user_id_best_rating pos_net_sentiment user_id_worst_rating neg_net_sentiment 
business123    user345    10    user789     -150 

再次澄清,使用dplyr ,它应该是由net_sentiment分数首先确定的最佳企业的列表,以及给出该业务的最佳和最差评级的用户,同样应该是应用对最坏的企业撒谎。

+2

一些样本数据会使得相当容易提供想法和/或答案。然而,一个想法是,期望你的结果能够保持在“高”格式,而不是你所建议的“宽”格式。虽然不难转换,但可视化和制作高分辨率格式要容易得多。 – r2evans

+0

@ r2evans添加了一些示例数据。 –

+0

你是否想要打破关系,还是想让所有结果回到最高分和最低分? –

回答

0

这是一个单管,可以让你第一个表;在那之后,诉诸会很容易地让你获得第二张桌子。如果你每次都拔掉头部,那么你就可以得到你想要的输出的单行。

逻辑基本上是按业务分组,并将最佳和最差结果变为自己的列,然后可以将该结果用作userID_best_rating的列的键。如果您从该密钥获取的结果太多,则将该商业ID作为辅助密钥添加(实质上利用每个用户ID的Score-BusiID组合密钥)。

管道为最高的正面评价和负面评价添加ID,然后在将最高评分排序到顶端之前对其进行修剪。

# simplified transportable data demonstrating similar pattern of overlap 
busiID <- c('a','b','c','b','e') 
userID <- c(1,1,1,2,1) 
netSenti <- c(80,78,77,74,72) 
ylp <- data.frame(busiID,userID,netSenti) 

SmryYlp <- 
    ylp %>% 
    group_by(busiID) %>% 
    mutate(pos_netSenti = max(netSenti), neg_netSenti = min(netSenti)) %>% 
    left_join(select(ylp, neg_netSenti = netSenti, user_id_worst_rating = userID)) %>% 
    left_join(select(ylp, pos_netSenti = netSenti, user_id_best_rating = userID)) %>% 
    select(busiID, user_id_best_rating, pos_netSenti, user_id_worst_rating, neg_netSenti) %>% 
    ungroup %>% distinct %>% 
    arrange(desc(pos_netSenti)) 

SmryYlp 
## A tibble: 4 × 5 
# busiID user_id_best_rating pos_netSenti user_id_worst_rating neg_netSenti 
# <fctr>    <dbl>  <dbl>    <dbl>  <dbl> 
# 1  a     1   80     1   80 
# 2  b     1   78     2   74 
# 3  c     1   77     1   77 
# 4  e     1   72     1   72 

希望这有助于

相关问题