【问题标题】:readSeries, read and convert time-series data set?readSeries,读取和转换时间序列数据集?
【发布时间】:2014-01-19 13:17:20
【问题描述】:

我在一个 csv 文件中包含一个数据集

      ,Southern Europe,EasternE
2000M1,99.2714858,94.0655995,
2000M2,99.28204201,95.20956637,
2000M3,99.42063947,95.99514288,
2000M4,99.86433479,96.81388546,
2000M5,99.91074036,97.52828582,
2000M6,99.58204075,98.87835592,
2000M7,99.50302486,100.970238,
2000M8,99.79380714,101.0939461,
2000M9,100.1770355,102.5641483,
2000M10,100.4298271,103.8086486,

我正在尝试使用 readSeries 将其读入 ts 对象,结果是 ts 对象,时间序列为

Jan 2000 99.27148 .......
Feb 2000 99.28204 .......
Mar 2000 99.42063 .......

我正在尝试使用 POSIX,但不知道是否可以让它识别这种 M1 M2 ... 格式,或者我是否必须在 csv 数据文件中进行一些巧妙的转换,然后再将其读入ts 对象?

欢迎提出任何建议。

谢谢

J

【问题讨论】:

    标签: r time-series


    【解决方案1】:

    1) timeSeries 包中的readSeries 生成"timesSeries" 类对象,而不是问题要求的"ts" 类对象。要创建 "ts" 对象,请尝试以下操作。 (顺便说一句,您的数据中的标题可能有误,但我只是按原样使用它们。之后使用colnames 修复它们。)

    Lines <- ",Southern Europe,EasternE
    2000M1,99.2714858,94.0655995,
    2000M2,99.28204201,95.20956637,
    2000M3,99.42063947,95.99514288,
    2000M4,99.86433479,96.81388546,
    2000M5,99.91074036,97.52828582,
    2000M6,99.58204075,98.87835592,
    2000M7,99.50302486,100.970238,
    2000M8,99.79380714,101.0939461,
    2000M9,100.1770355,102.5641483,
    2000M10,100.4298271,103.8086486,
    "
    
    # replace text=Lines with something like "myfile.csv"
    DF <- read.csv(text = Lines, check.names = FALSE)
    ts(DF, start = c(2000, 1), freq = 12)
    

    给出:

                       Southern Europe EasternE
    Jan 2000  99.27149        94.06560       NA
    Feb 2000  99.28204        95.20957       NA
    Mar 2000  99.42064        95.99514       NA
    Apr 2000  99.86433        96.81389       NA
    May 2000  99.91074        97.52829       NA
    Jun 2000  99.58204        98.87836       NA
    Jul 2000  99.50302       100.97024       NA
    Aug 2000  99.79381       101.09395       NA
    Sep 2000 100.17704       102.56415       NA
    Oct 2000 100.42983       103.80865       NA
    

    2) 动物园 这也有效:

    library(zoo)
    FUN <- function(x) as.yearmon(x, "%YM%m")
    tt <- as.ts(read.zoo(text = Lines, skip = 1, sep = ",", FUN = FUN))
    

    要更改列名,之后可以这样做:

    colnames(tt) <- c(...whatever...)
    

    【讨论】:

    • 谢谢,解决方案 2) 是一个非常优雅的解决方案,似乎效果很好。给我带来麻烦的是如何处理中间的“M”占位符,但这解决了!
    猜你喜欢
    • 1970-01-01
    • 2020-01-21
    • 2019-09-19
    • 2020-06-07
    • 1970-01-01
    • 2020-06-13
    • 2019-06-16
    • 1970-01-01
    相关资源
    最近更新 更多