如何将ts转换为data.frame?

时间:2013-11-17 02:54:03

标签: r

> print( ts(as.character(seq(as.Date("2013-9-1"),length.out=30,by=1)), frequency = 7, start = c(1, 7)), calendar = TRUE) 
          p1         p2         p3         p4         p5         p6         p7
1                                                                   2013-09-01
2 2013-09-02 2013-09-03 2013-09-04 2013-09-05 2013-09-06 2013-09-07 2013-09-08
3 2013-09-09 2013-09-10 2013-09-11 2013-09-12 2013-09-13 2013-09-14 2013-09-15
4 2013-09-16 2013-09-17 2013-09-18 2013-09-19 2013-09-20 2013-09-21 2013-09-22
5 2013-09-23 2013-09-24 2013-09-25 2013-09-26 2013-09-27 2013-09-28 2013-09-29
6 2013-09-30   

我希望从ts获得一个data.frame并具有两个功能:
1.rownames是1 2 3 4 5 6
2.colnames是星期一星期三星期四星期四星期六星期六星期日 我怎么能得到它?

    Mon             Tue        Wed        Thu        Fri       Sat         Sun 
1                                                                   2013-09-01
2 2013-09-02 2013-09-03 2013-09-04 2013-09-05 2013-09-06 2013-09-07 2013-09-08
3 2013-09-09 2013-09-10 2013-09-11 2013-09-12 2013-09-13 2013-09-14 2013-09-15
4 2013-09-16 2013-09-17 2013-09-18 2013-09-19 2013-09-20 2013-09-21 2013-09-22
5 2013-09-23 2013-09-24 2013-09-25 2013-09-26 2013-09-27 2013-09-28 2013-09-29
6 2013-09-30 

也许这是从我的代码中传输data.frame的最快捷方式。

2 个答案:

答案 0 :(得分:0)

这应该有效:

time.df<-data.frame(date=as.Date(c(time)))
time.df$day<-strftime(time.df$date,'%A')
time.df$year.week<-strftime(time.df$date,'%Y-%W') # Monday starts week.
# Just to avoid locale differences, get the names of the days of week in current locale.
dows<-strftime(seq(as.Date('2013-11-18'),(as.Date('2013-11-18')+6),by=1),'%A')
dow.order<-paste('date',dows,sep='.')
calendar<-reshape(time.df,idvar='year.week',timevar='day',direction='wide') [dow.order]
rownames(calendar)<-NULL
colnames(calendar)<-dows
calendar
#       Monday    Tuesday  Wednesday   Thursday     Friday   Saturday     Sunday
# 1       <NA>       <NA>       <NA>       <NA>       <NA>       <NA> 2013-09-01
# 2 2013-09-02 2013-09-03 2013-09-04 2013-09-05 2013-09-06 2013-09-07 2013-09-08
# 3 2013-09-09 2013-09-10 2013-09-11 2013-09-12 2013-09-13 2013-09-14 2013-09-15
# 4 2013-09-16 2013-09-17 2013-09-18 2013-09-19 2013-09-20 2013-09-21 2013-09-22
# 5 2013-09-23 2013-09-24 2013-09-25 2013-09-26 2013-09-27 2013-09-28 2013-09-29
# 6 2013-09-30       <NA>       <NA>       <NA>       <NA>       <NA>       <NA>

但我想知道为什么你会需要这个。

答案 1 :(得分:0)

我会尝试这样的事情:

## Your daily time series data
out <- ts(as.character(seq(as.Date("2013-9-1"),
          length.out = 30, by = 1)), 
          frequency = 7, start = c(1, 7))

## Comes in useful later
WD <- c("Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday")

## Create your data as a long data.frame
## Extract the weekdays using the weekdays function
out2 <- data.frame(weekday = weekdays(as.Date(as.character(out))), out)

## Use cumsum to determine the weeks. We'll start our weeks on Monday
out2$week <- cumsum(out2$weekday == "Monday")

## This is your new "long" dataset
head(out2)
#     weekday        out week
# 1    Sunday 2013-09-01    0
# 2    Monday 2013-09-02    1
# 3   Tuesday 2013-09-03    1
# 4 Wednesday 2013-09-04    1
# 5  Thursday 2013-09-05    1
# 6    Friday 2013-09-06    1

从那里开始,很容易“重塑”您的数据(使用基础R的reshape,或者更方便地使用“{1}}来自”reshape2“。

dcast