为什么R中的平均价格没有按房间类型正确分组?

ntjbwcob  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(99)

我有以下问题。我希望每个房间类型显示平均价格,但所有行中显示的价格相同,并且表中的级别仍然重复。就好像分组没有发生过一样。

room_type <- c("Entire home/apt","Private room","Shared room","Hotel room","Entire home/apt",         "Private room","Shared room","Hotel room")

price <- c(100, 200, 50, 3000, 80, 90, 56, 30)

listing <- data.frame(room_type, price)
print(listing)


df_2 <- listing %>% 
  group_by(room_type) %>%
  mutate(price = mean(price)) %>%
  summarise(room_type = room_type, avg_price = price)
df_2
z9smfwbn

z9smfwbn1#

这是你要找的吗?

listing %>% 
  group_by(room_type) %>% 
  summarise(avg_price=mean(price))

这是结果

相关问题