[R] date and matrices
arun
smartpink111 at yahoo.com
Wed Feb 13 15:37:22 CET 2013
Hi Elisa,
Try this:
date1<-format(seq.Date(as.Date("1991.1.1",format="%Y.%m.%d"),as.Date("1996.12.31",format="%Y.%m.%d"),by="day"),"%Y.%m.%d")
length(date1)
#[1] 2192
mat1<-matrix(c(.314,.314,.273,.273,.236,.236,.236,.236,.273,.314,.403,.314),ncol=1)
res1<- as.data.frame(do.call(rbind,lapply(split(date1,((seq_along(date1)-1)%/%12)+1),function(x){if(length(x)!=12) cbind(x,head(mat1,length(x))) else cbind(x,mat1)})),stringsAsFactors=FALSE)
res1[,2]<- sapply(res1[,2],as.numeric)
str(res1)
#'data.frame': 2192 obs. of 2 variables:
# $ x : chr "1991.01.01" "1991.01.02" "1991.01.03" "1991.01.04" ...
# $ V2: num 0.314 0.314 0.273 0.273 0.236 0.236 0.236 0.236 0.273 0.314 ...
head(res1)
# x V2
#1 1991.01.01 0.314
#2 1991.01.02 0.314
#3 1991.01.03 0.273
#4 1991.01.04 0.273
#5 1991.01.05 0.236
#6 1991.01.06 0.236
tail(res1)
# x V2
#2187 1996.12.26 0.273
#2188 1996.12.27 0.273
#2189 1996.12.28 0.236
#2190 1996.12.29 0.236
#2191 1996.12.30 0.236
#2192 1996.12.31 0.236
res1$x<- as.Date(res1$x,format="%Y.%m.%d")
str(res1)
#'data.frame': 2192 obs. of 2 variables:
# $ x : Date, format: "1991-01-01" "1991-01-02" ...
# $ V2: num 0.314 0.314 0.273 0.273 0.236 0.236 0.236 0.236 0.273 0.314 ...
A.K.
________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
Sent: Wednesday, February 13, 2013 9:01 AM
Subject: date and matrices
Dear Arun,
[text files is also attached to be used in case format is changed]
i have 124 matrices with 12rows and 1 column. In all those 124 columns there are values of a certain quantity called disty. what i want to do is the following
1- insert a date column in each matrix in such a way that date should start from 1991. 1. 1 (yyyy. m. d) and should end at
1996.12.31.
2- against each date i want to put the values of quantity disty and as i have only 12 values of disty i want to start from the
first value after very 12 days.
more precisely, if i have a matrix like this
mat1
[,1]
.314
.314
.273
.273
.236
.236
.236
.236
.273
.314
.403
.314
i want it to be like the following one
1911. 1. 1 .314
1911. 1. 2 .314
1911. 1. 3 .273
1911. 1. 4 .273
1911. 1. 5 .236
1911. 1. 6 .236
1911. 1. 7 .236
1911. 1. 8 .236
1911. 1. 9 .273
1911. 1.10 2.314
1911. 1.11 12.403
1911. 1.12 .314
1911. 1.11 .314
1911. 1.12 .314
1911. 1.13 .273
1911. 1.14 .273
1911. 1.15 .236
1911. 1.16 .236
1911. 1.17 .236
1911. 1.18 .236
1911. 1.19 .273
1911. 1.20 2.314
1911. 1.21 12.403
1911. 1.22 .314
1911. 1.23 .314
1911. 1.24 .314
1911. 1.25 .273
1911. 1.26 .273
1911. 1.27 .236
1911. 1.28 .236
1911. 1.29 .236
1911. 1.30 .236
1911. 1.31 .273
1911. 2. 1 2.314
1911. 2. 2 12.403
1911. 2. 3 .314
1911. 2. 4 .314
1911. 2. 5 .314
1911. 2. 6 .273
1911. 2. 7 .273
1911. 2. 8 .236
1911. 2. 9 .236
1911. 2.10 .236
1911. 2.11 .236
1911. 2.12 .273
1911. 2.13 2.314
1911. 2.11 12.403
----------------------
----------------------
1911.12.17 12.403
------------------------
------------------------
------------------------
and so on till 1996
i also want these 124 matrices to be saved in my computer with the pattern exactly shown above. you can see if a value is in decimal number it is exactly 6 spaces away from the day number in date column. these spaces are subsequently reduced when the values go from decimal to higher.
Actually it wont be wrong to say the pattren of output text files saved in my computer should stricktly carry all the patterns as given above as i will later on use them in fortran.
More information about the R-help
mailing list