我在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对象,结果是a ts对象,时间序列为
Jan 2000 99.27148 .......
Feb 2000 99.28204 .......
Mar 2000 99.42063 .......
我正在尝试使用POSIX,但不知道是否有可能让它识别这个M1 M2 ......格式,或者如果我必须在csv数据文件中进行一些聪明的转换,然后再将其读入ts对象?
欢迎提出任何建议。
由于
Ĵ
答案 0 :(得分:0)
1) 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...)