Skip to content

date and matrices

1 message · arun

#
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.