首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子集时序数据

子集时序数据
EN

Stack Overflow用户
提问于 2016-05-11 04:27:12
回答 4查看 565关注 0票数 1
代码语言:javascript
复制
ggplot(Price.data['2000-01/2015-12'],aes(x=Demand,y=Price))+geom_point()+geom_smooth(method=lm)

indexClass(Price.data)
[1] "Date"

如何仅绘制2010-2014年3、4、6月份的数据?

代码语言:javascript
复制
 head(Price.data)
        Dry_Gas_Y Power_Gas_Y Price_Gas_Y
1990-01-01  52.16720    5.469179        2.39
1990-02-01  51.45287    5.470755        1.90
1990-03-01  49.29829    6.908609        1.55
1990-04-01  48.29243    7.721371        1.49
1990-05-01  47.25959    9.154057        1.47
1990-06-01  47.48744   11.525595        1.47
EN

回答 4

Stack Overflow用户

发布于 2016-05-11 05:25:18

代码语言:javascript
复制
library(tidyverse)

Price.data %>% 
  mutate(year = as.numeric(format(Date, "%Y")),
         month = as.numeric(format(Date, "%m"))) %>%
  filter(year > 2009 & year < 2015, month == 3 | month == 4 | month ==6) %>%     
ggplot(aes(Demand,Price))+geom_point()+geom_smooth(method=lm)
票数 1
EN

Stack Overflow用户

发布于 2016-05-11 05:21:29

您可以使用data.table,这可能是最快的解决方案

代码语言:javascript
复制
library(data.table)   


# convert your dataset into a data.table
  setDT(df)

# If necessary, get date column into date format
#  df[ , Date := as.Date(df$Date, "%m-%d-%y") ] 

# Create separate columns for year and month
  df[, year := year(Date)][, month := month(Date)]

# filter dataset
  df <- df[ month %in% c(3,4,6) & year %in% c(2009:2014), ]
  #  subset(df, month %in% c(3,4,6) & year %in% c(2009:2014) ) # you could also use a simple subset, but this is likely to be slower

绘图

代码语言:javascript
复制
  ggplot(data=df, aes(x=Demand, y=Price)) + geom_point() + geom_smooth(method=lm)
票数 0
EN

Stack Overflow用户

发布于 2016-05-11 09:15:14

在您的示例中,我没有看到日期有列名,看起来日期就是行名。出于这个原因,这个示例创建了一个'Date‘列,然后创建了'Month’和'Year‘列,以便您随后过滤日期。

代码语言:javascript
复制
library(lubridate)
library(dplyr

plot_months <- Price.data%>%
               mutate(Date = row.names(.),
                      Month = month(Date),
                      Year = year(Date))%>%
               filter(Month %in% c(3,4,6),
                      Year %in% c(2009:2014))

ggplot(plot_months, aes(x=Demand,y=Price))+
       geom_point()+
       geom_smooth(method=lm)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37148324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档