简体   繁体   English

在 r 中熔化重塑 2

[英]Melt reshape 2 in r

I need help to melt my data set such that I can plot the average resale price of each region using line graphs in ggplot2 which I intend to put into a shiny app我需要帮助来melt我的数据集,以便我可以使用ggplot2中的折线图绘制每个区域的平均转售价格,我打算将其放入一个shiny应用程序中

Here's an example of my data set.这是我的数据集的一个例子。

town           flat_model resale_price year region
1 ANG MO KIO   MODEL A      345000     2007 North_East
2 BISHAN       MODEL A      336000     2009 Central
3 SEMBAWANG    MODEL A      385000     2010 North

Here's the graph which I intend to make.这是我打算制作的图表。 I made this using Tableau我是用 Tableau 做的图片

I just need help with melting the data set so that I can carry on.我只需要帮助融化数据集,以便我可以继续。 Thanks!谢谢!

I'm pretty sure you dont need to melt your data, it's already in long format (one variable per column, one observation per row).我很确定你不需要melt你的数据,它已经是长格式(每列一个变量,每行一个观察)。

If you want an average resale price for each region and year, you want to group_by and summarize , something along the lines of:如果您想要每个地区和年份的平均转售价格,您需要group_bygroup_bygroup_by summarize

df %>% 
  group_by(year, region) %>% 
  summarize(mean_price = mean(resale_price))

Using this as example data, I get something which would allow you to plot the regional annual means.使用这个作为示例数据,我得到了一些可以让你绘制区域年度平均值的东西。

df1 <- data.frame(
  'year' = c(1,1,2,2,2),
  'region' = c('A','B','A','B','B'),
  'resale_price' = c(4,7,5,9,8))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM