r - why all date strings are changed into numbers? -
start=as.date("2013-09-02") x[1:140]<-start for(i in 2:140){ x[i]<-x[i-1]+1 } what such:
[1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" when run :
y<-matrix(x,nrow=20,byrow=true) > y [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 15950 15951 15952 15953 15954 15955 15956 all string changed numbers,why?how can not change them in y?
the problem r doesn't allow matrices populated dates. both "matrix" , "date" classes. underlying representation of date object in r integer. when create matrix y, takes underlying data x (which array of integers), adds dimension attribute, , makes class "matrix".
there's not clean way around problem. there few hacks can use, though. example, explicitly force y of class "date" "matrix" class(y)<-c("matrix","date"). y still print out vector of dates, able manipulate using matrix cordinates:
> class(y)<-c("matrix","date") > head(y) [1] "2013-09-02" "2013-09-09" "2013-09-16" "2013-09-23" "2013-09-30" [6] "2013-10-07" "2013-09-03" "2013-09-10" "2013-09-17" "2013-09-24" [11] "2013-10-01" "2013-10-08" "2013-09-04" "2013-09-11" "2013-09-18" [16] "2013-09-25" "2013-10-02" "2013-10-09" "2013-09-05" "2013-09-12" [21] "2013-09-19" "2013-09-26" "2013-10-03" "2013-10-10" "2013-09-06" [26] "2013-09-13" "2013-09-20" "2013-09-27" "2013-10-04" "2013-10-11" [31] "2013-09-07" "2013-09-14" "2013-09-21" "2013-09-28" "2013-10-05" [36] "2013-10-12" "2013-09-08" "2013-09-15" "2013-09-22" "2013-09-29" [41] "2013-10-06" "2013-10-13" > y[1,] [1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" [6] "2013-09-07" "2013-09-08" > y[1,]<-y[1,]+1 > y[1,] [1] "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" "2013-09-07" [6] "2013-09-08" "2013-09-09" you use data frame instead of matrix:
> y<-data.frame(y) > y<-data.frame(lapply(y,function(x) {class(x)<-"date";x})) > head(y) x1 x2 x3 x4 x5 x6 x7 1 2013-09-02 2013-09-03 2013-09-04 2013-09-05 2013-09-06 2013-09-07 2013-09-08 2 2013-09-09 2013-09-10 2013-09-11 2013-09-12 2013-09-13 2013-09-14 2013-09-15 3 2013-09-16 2013-09-17 2013-09-18 2013-09-19 2013-09-20 2013-09-21 2013-09-22 4 2013-09-23 2013-09-24 2013-09-25 2013-09-26 2013-09-27 2013-09-28 2013-09-29 5 2013-09-30 2013-10-01 2013-10-02 2013-10-03 2013-10-04 2013-10-05 2013-10-06 6 2013-10-07 2013-10-08 2013-10-09 2013-10-10 2013-10-11 2013-10-12 2013-10-13 a third possiblity keep elements in y in character format, , convert them dates using as.date when need them:
> y<-matrix(as.character(x),nrow=20,byrow=t) > head(y) [,1] [,2] [,3] [,4] [,5] [1,] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" [2,] "2013-09-09" "2013-09-10" "2013-09-11" "2013-09-12" "2013-09-13" [3,] "2013-09-16" "2013-09-17" "2013-09-18" "2013-09-19" "2013-09-20" [4,] "2013-09-23" "2013-09-24" "2013-09-25" "2013-09-26" "2013-09-27" [5,] "2013-09-30" "2013-10-01" "2013-10-02" "2013-10-03" "2013-10-04" [6,] "2013-10-07" "2013-10-08" "2013-10-09" "2013-10-10" "2013-10-11" [,6] [,7] [1,] "2013-09-07" "2013-09-08" [2,] "2013-09-14" "2013-09-15" [3,] "2013-09-21" "2013-09-22" [4,] "2013-09-28" "2013-09-29" [5,] "2013-10-05" "2013-10-06" [6,] "2013-10-12" "2013-10-13" > as.date(y[1,]) [1] "2013-09-02" "2013-09-03" "2013-09-04" "2013-09-05" "2013-09-06" [6] "2013-09-07" "2013-09-08"
Comments
Post a Comment