I have a dataframe with several columns:
我有一个包含多个列的数据框:
- state
- county
- year
Then x, y, and z, where x, y, and z are observations unique to the triplet listed above. I am looking for a sane way to store this in a time series and xts will not let me since there are multiple observations for each time index. I have looked at the hts package, but am having trouble figuring out how to get my data into it from the dataframe.
然后是x,y和z,其中x,y和z是上面列出的三联体特有的观察结果。我正在寻找一种理智的方式来存储这个时间序列,xts不会让我,因为每个时间索引有多个观察。我查看了hts包,但是无法弄清楚如何从数据帧中获取数据。
(Yes, I did post the same question on Quora, and was advised to bring it here!)
(是的,我确实在Quora上发布了同样的问题,并建议将它带到这里!)
1 个解决方案
#1
8
One option is to reshape your data so you have a column for every State-County combination. This allows you to construct an xts matrix :
一种选择是重塑您的数据,以便为每个州 - 县组合提供一列。这允许您构造一个xts矩阵:
require(reshape)
Opt1 <- as.data.frame(cast(Data, Date ~ county + State, value="Val"))
rownames(Opt1) <- Opt1$Date
Opt1$Date <- NULL
as.xts(Opt1)
Alternatively, you could work with a list of xts objects, each time making sure that you have the correct format as asked by xts. Same goes for any of the other timeseries packages. A possible solution would be :
或者,您可以使用xts对象列表,每次都确保您具有xts要求的正确格式。任何其他时间序列包也是如此。一个可能的解决方案是:
Opt2 <-
with(Data,
by(Data,list(county,State,year),
function(x){
rownames(x) <- x$Date
x <- x["Val"]
as.xts(x)
}
)
)
Which would allow something like :
这将允许类似的东西:
Opt2[["d","b","2012"]]
to select a specific time series. You can use all xts options on that. You can loop through the counties, states and years to construct plots like this one :
选择特定的时间序列。您可以使用所有xts选项。您可以遍历县,州和年来构建像这样的图:
Code for plot :
情节代码:
counties <- dimnames(Opt2)[[1]]
states <- dimnames(Opt2)[[2]]
years <- dimnames(Opt2)[[3]]
op <- par(mfrow=c(3,6))
apply(
expand.grid(counties,states,years),1,
function(i){
plot(Opt2[[i[1],i[2],i[3]]],main=paste(i,collapse="-"))
invisible()
}
)
par(op)
Test-data :
Data <- data.frame( State = rep(letters[1:3],each=90),
county = rep(letters[4:6],90),
Date = rep(seq(as.Date("2011-01-01"),by="month",length.out=30),each=3),
Val = runif(270)
)
Data$year <- as.POSIXlt(Data$Date)$year + 1900
#1
8
One option is to reshape your data so you have a column for every State-County combination. This allows you to construct an xts matrix :
一种选择是重塑您的数据,以便为每个州 - 县组合提供一列。这允许您构造一个xts矩阵:
require(reshape)
Opt1 <- as.data.frame(cast(Data, Date ~ county + State, value="Val"))
rownames(Opt1) <- Opt1$Date
Opt1$Date <- NULL
as.xts(Opt1)
Alternatively, you could work with a list of xts objects, each time making sure that you have the correct format as asked by xts. Same goes for any of the other timeseries packages. A possible solution would be :
或者,您可以使用xts对象列表,每次都确保您具有xts要求的正确格式。任何其他时间序列包也是如此。一个可能的解决方案是:
Opt2 <-
with(Data,
by(Data,list(county,State,year),
function(x){
rownames(x) <- x$Date
x <- x["Val"]
as.xts(x)
}
)
)
Which would allow something like :
这将允许类似的东西:
Opt2[["d","b","2012"]]
to select a specific time series. You can use all xts options on that. You can loop through the counties, states and years to construct plots like this one :
选择特定的时间序列。您可以使用所有xts选项。您可以遍历县,州和年来构建像这样的图:
Code for plot :
情节代码:
counties <- dimnames(Opt2)[[1]]
states <- dimnames(Opt2)[[2]]
years <- dimnames(Opt2)[[3]]
op <- par(mfrow=c(3,6))
apply(
expand.grid(counties,states,years),1,
function(i){
plot(Opt2[[i[1],i[2],i[3]]],main=paste(i,collapse="-"))
invisible()
}
)
par(op)
Test-data :
Data <- data.frame( State = rep(letters[1:3],each=90),
county = rep(letters[4:6],90),
Date = rep(seq(as.Date("2011-01-01"),by="month",length.out=30),each=3),
Val = runif(270)
)
Data$year <- as.POSIXlt(Data$Date)$year + 1900