[R] date and matrices
arun
smartpink111 at yahoo.com
Wed Feb 13 19:06:57 CET 2013
HI Elisa,
You also mentioned about more than 100 matrices with 1 column each.
Suppose, if the matrices are named from mat1:mat4 (In your case mat1:mat124)
mat1<-matrix(c(.314,.314,.273,.273,.236,.236,.236,.236,.273,.314,.403,.314),ncol=1)
mat2<-matrix(c(.314,.325,.246,.273,.226,.236,.249,.236,.289,.314,.403,.314),ncol=1)
mat3<-matrix(c(.314,.320,.275,.273,.236,.232,.236,.231,.273,.315,.413,.314),ncol=1)
mat4<-matrix(c(.318,.327,.295,.273,.286,.236,.264,.236,.295,.315,.423,.314),ncol=1)
res3<- lapply(paste0("mat",1:4),function(i) data.frame(date1,val=c(rep(get(i),182),head(get(i),8)),stringsAsFactors=FALSE))
#change from 1:124
str(res3)
#List of 4
# $ :'data.frame': 2192 obs. of 2 variables:
# ..$ date1: chr [1:2192] "1991.01.01" "1991.01.02" "1991.01.03" "1991.01.04" ...
# ..$ val : num [1:2192] 0.314 0.314 0.273 0.273 0.236 0.236 0.236 0.236 0.273 0.314 ...
#$ :'data.frame': 2192 obs. of 2 variables:
# ..$ date1: chr [1:2192] "1991.01.01" "1991.01.02" "1991.01.03" "1991.01.04" ...
# ..$ val : num [1:2192] 0.314 0.325 0.246 0.273 0.226 0.236 0.249 0.236 0.289 0.314 ...
#$ :'data.frame': 2192 obs. of 2 variables:
#..$ date1: chr [1:2192] "1991.01.01" "1991.01.02" "1991.01.03" "1991.01.04" ...
#..$ val : num [1:2192] 0.314 0.32 0.275 0.273 0.236 0.232 0.236 0.231 0.273 0.315 ...
#$ :'data.frame': 2192 obs. of 2 variables:
#..$ date1: chr [1:2192] "1991.01.01" "1991.01.02" "1991.01.03" "1991.01.04" ...
# ..$ val : num [1:2192] 0.318 0.327 0.295 0.273 0.286 0.236 0.264 0.236 0.295 0.315 ...
head(res3[[1]])
# date1 val
#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
I am not sure whether you could do it in one step as memory might be an issue here. You could split this to 2-3 times:
1:25, 24:50, and so on..
A.K.
________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
Sent: Wednesday, February 13, 2013 11:44 AM
Subject: RE: date and matrices
Dear Arun,
When i save the matrices in text file there are inverted commas on each date and value like the following
"1991.01.01" "0.314"
"1991.01.02" "0.314"
"1991.01.03" "0.273"
"1991.01.04" "0.273"
"1991.01.05" "0.236"
"1991.01.06" "0.236"
how to eliminate these inverted commas?
1991.01.01 0.314
1991.01.02 0.314
1991.01.03 0.273
1991.01.04 0.273
1991.01.05 0.236
1991.01.06 0.236
thanks
elisa
========================
> Date: Wed, 13 Feb 2013 06:53:43 -0800
> From: smartpink111 at yahoo.com
> Subject: Re: date and matrices
> To: eliza_botto at hotmail.com
> CC: r-help at r-project.org
>
> Hi,
>
> Just forgot about one thing:
> With more matrices with one column:
> One way would be to combine them as a single matrix:
> 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")
>
> mat1<-matrix(c(.314,.314,.273,.273,.236,.236,.236,.236,.273,.314,.403,.314),ncol=1)
> mat2<-matrix(c(.314,.325,.246,.273,.226,.236,.249,.236,.289,.314,.403,.314),ncol=1)
> mat3<- cbind(mat1,mat2) #if this is not feasible, tell me how these matrices are named
>
> res2<- lapply(seq_len(ncol(mat3)),function(i) cbind(date1,c(rep(mat3[,i],182),head(mat3[,i],8))))
> head(res2[[1]])
> # date1
> #[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(res2[[1]])
> # date1
> #[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"
>
>
> 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