Varun Raghav B Varun Raghav B - 22 days ago 10
R Question

How do I find group-wise mean in R?

The head of my dataset looks like this.

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


I want to find the average sentiment for each hotel. Like

Hotel_Name Sentiment

X 2.6

Y 2.2

Answer Source

This looks like a good use case for dplyr

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