2017-09-26 49 views
-5

我的數據集的頭像是這樣的。如何在R中找到group-wise的意思?

Hotel_name       Review_Title Sentiment 
1 Accord Metropolitan Excellent comfortableness during stay   3 
2 Accord Metropolitan Excellent comfortableness during stay   3 
3 Accord Metropolitan Excellent comfortableness during stay   3 
4 Accord Metropolitan Excellent comfortableness during stay   3 
5 Accord Metropolitan Excellent comfortableness during stay   3 
6 Accord Metropolitan     Not too comfortable   1 
    Rating_Percentage X X.1 X.2 X.3  word 
1    100 NA  NA NA  nice 
2    100 NA  NA NA  stay 
3    100 NA  NA NA business 
4    100 NA  NA NA tourist 
5    100 NA  NA NA purpose 
6    20 NA  NA NA hotel 

我想找到每家酒店的平均情緒。像
HOTEL_NAME情緒
X 2.6
Ÿ2.2

回答

0

這看起來就像dplyr的好用例

library("dplyr") 
data %>% 
    group_by(Hotel_name) %>% 
    summarise(mean_sentiment = mean(Sentiment)) 
+0

我得到Na作爲輸出以及此警告。 –

+0

1:在mean.default(Sentiment)中:參數不是數字或邏輯:返回不適用 –

+0

這可能是因爲Sentiment不是數字變量(嘗試'class(data $ Sentiment)' - 我的猜測是它被讀入作爲一個因素?您可以將上述內容更新爲: 'data%>% mutate(Sentiment = as.numeric(Sentiment))%>% group_by(Hotel_name)%>% summary(mean_sentiment = mean(Sentiment)) ' – Lucy

0

您可以使用data.table包,改變你的數據data.table通過setDT

library(data.table) 
setDT(data)[,.(Sentiment= mean(Sentiment)),by=Hotel_Name]