如何将R中的因子数据类型转换为日期

时间:2014-07-23 18:05:32

标签: r time-series

我的数据如下:

Date    department_id   position_id totala  Weekday start_Time  Date    Month   Year
7/12/2014   7487        126476      218.39     5    956           12    7       2014
7/7/2014    7487        126476       270.28    7    855            7    7       2014
7/8/2014    7487        126476       247.83    1    810            8    7       2014
7/9/2014    7487        126476        191.16    2   959            9    7       2014
7/9/2014    7487        126476        191.16    2   627             9   7       2014

........

当我尝试将其转换为时间序列数据时,它会给我这个错误:

Error in as.POSIXlt.character(x, tz, ...) : 
character string is not in a standard unambiguous format

str (ant):

'data.frame':   29 obs. of  5 variables:
 **$ Date         : Factor w/ 12 levels "6/29/2014","6/30/2014",..: 5 10 11 12 12 4 9 8 7 3** ...
 $ department_id: int  7487 7487 7487 7487 7487 7487 7487 7487 7487 7487 ...
 $ position_id  : int  126476 126476 126476 126476 126476 126476 126476 126476 126476 126476 ...
 $ totala       : num  218 270 248 191 191 ...
 $ start_Time   : int  956 855 810 959 627 864 630 742 948 623 ...

1 个答案:

答案 0 :(得分:1)

 mydf = read.table(header=T, text="
+ Date    department_id   position_id totala  Weekday start_Time  Date    Month   Year
+ 7/12/2014   7487        126476      218.39     5    956           12    7       2014
+ 7/7/2014    7487        126476       270.28    7    855            7    7       2014
+ 7/8/2014    7487        126476       247.83    1    810            8    7       2014
+ 7/9/2014    7487        126476        191.16    2   959            9    7       2014
+ 7/9/2014    7487        126476        191.16    2   627             9   7       2014
+ ")
> 
> mydf
       Date department_id position_id totala Weekday start_Time Date.1 Month
1 7/12/2014          7487      126476 218.39       5        956     12     7
2  7/7/2014          7487      126476 270.28       7        855      7     7
3  7/8/2014          7487      126476 247.83       1        810      8     7
4  7/9/2014          7487      126476 191.16       2        959      9     7
5  7/9/2014          7487      126476 191.16       2        627      9     7
  Year
1 2014
2 2014
3 2014
4 2014
5 2014
> 
> 
> str(mydf)
'data.frame':   5 obs. of  9 variables:
 $ Date         : Factor w/ 4 levels "7/12/2014","7/7/2014",..: 1 2 3 4 4
 $ department_id: int  7487 7487 7487 7487 7487
 $ position_id  : int  126476 126476 126476 126476 126476
 $ totala       : num  218 270 248 191 191
 $ Weekday      : int  5 7 1 2 2
 $ start_Time   : int  956 855 810 959 627
 $ Date.1       : int  12 7 8 9 9
 $ Month        : int  7 7 7 7 7
 $ Year         : int  2014 2014 2014 2014 2014
> 
> 


> mydf$Date = as.Date(mydf$Date, format="%m/%d/%Y")
> 
> mydf
        Date department_id position_id totala Weekday start_Time Date.1 Month Year
1 2014-07-12          7487      126476 218.39       5        956     12     7 2014
2 2014-07-07          7487      126476 270.28       7        855      7     7 2014
3 2014-07-08          7487      126476 247.83       1        810      8     7 2014
4 2014-07-09          7487      126476 191.16       2        959      9     7 2014
5 2014-07-09          7487      126476 191.16       2        627      9     7 2014
> str(mydf)
'data.frame':   5 obs. of  9 variables:
 $ Date         : Date, format: "2014-07-12" "2014-07-07" "2014-07-08" "2014-07-09" ...
 $ department_id: int  7487 7487 7487 7487 7487
 $ position_id  : int  126476 126476 126476 126476 126476
 $ totala       : num  218 270 248 191 191
 $ Weekday      : int  5 7 1 2 2
 $ start_Time   : int  956 855 810 959 627
 $ Date.1       : int  12 7 8 9 9
 $ Month        : int  7 7 7 7 7
 $ Year         : int  2014 2014 2014 2014 2014
>