[R] date and matrices
arun
smartpink111 at yahoo.com
Wed Feb 13 18:51:37 CET 2013
Dear Elisa,
When you save it as matrix, if there is atleast one column with character class, others will be converted to that class. It would be better to store it as data.frame
res2<-lapply(seq_len(ncol(mat3)),function(i) data.frame(date1,val=c(rep(mat3[,i],182),head(mat3[,i],8)),stringsAsFactors=FALSE))
str(res2)
#List of 2
# $ :'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 ...
head(res2[[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
If I convert this back to matrix.
str(lapply(res2,as.matrix))
List of 2
$ : chr [1:2192, 1:2] "1991.01.01" "1991.01.02" "1991.01.03" "1991.01.04" ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:2] "date1" "val"
$ : chr [1:2192, 1:2] "1991.01.01" "1991.01.02" "1991.01.03" "1991.01.04" ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr [1:2] "date1" "val" # both are now of class "chr"
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