我的数据集的头部看起来像这样。
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 :(得分:1)
这看起来像是dplyr的一个很好的用例
library("dplyr")
data %>%
group_by(Hotel_name) %>%
summarise(mean_sentiment = mean(Sentiment))
答案 1 :(得分:0)
您可以使用 data.table 包,通过 setDT 将数据更改为 data.table :
library(data.table)
setDT(data)[,.(Sentiment= mean(Sentiment)),by=Hotel_Name]