convert several list objects in a matrix to an actual list
Pergunta
I extracted daily temperature data over a time span of 45 years. I used all possibilities I could think of to append, cbind, rbind those data into a variable that grows every year loop. Besides the value I also included the time (as.Date). My final variable unfortunately consists of 45 rows and 2 columns with each cell containing the value(left column) and the date (right column). However, I cannot find a way to 'expand' the cells to gain two columns containing one value per cell.
At first I tried to get a better variable from the loop with merge(, by='date') but it is not working since I only have the dates of a single year. I am looking for a command that either expands the cells, or, even better, that would really append the two columns (value & date) at the bottom of my variable inside the loop as single entries for each cell.
The input data is a text file with the year-blocks (leading row with the year written e.g. 1960, and a 31 x 13 matrix) are written below each other. -9999 are my NA-flags.
1960 - - - - - - - - - - - -
1 -22.2 -13.5 -6.2 -5.4 . . . x(1,13)
2 -22.4 -15.9 -5.7 7.6 . . . x(2,13)
.
.
.
31
30 -9.9 -9999 -8 4.8 . . . x(30,13)
31 -17 -9999 -6.2 . . . x(31,13)
1961 - - - - - - - - - - - -
1 -17.8 -22.6 -11.7 -0.5 4 11.9 10.4 14.8 12 -0.1 -9.2 -16.3
The code simplified:
dat1 <- data.frame(read.table(filename))
dat1$NUM <- 1:length(dat1$V1) #I needed an index
TYs <- 1960 # Year start
TYe <- 2005 # Year end
TYi–TYs
TMP3 <- NULL #The variable that should store the data. Append new data every loop
while (TYi <= TYe){
index <- dat1$NUM[dat1$V1==TYi]
# get the start and stop of matrix indices
begin <- index+1
end <- begin+30
oddyear <-format(as.Date(paste('3112',TYi),'%d%m%Y'),'%j')== '366' #checks if TYi is oddyear
if (oddyear==TRUE){
date <- seq(as.Date(paste('0101',TYi), '%d%m%Y'),as.Date(paste('3112',TYi), '%d%m%Y'),'day')
TMP2 <- NULL
TMP2$data[1:31] <- TMP[1:31]
TMP2$data[32:60] <- TMP[32:60]
#...
TMP2$data[336:366] <- TMP[342:372]
TMP2$date <- date
TMP3 <- rbind(TMP3, TMP2)
TYi <- TYi+1
TMP2 <- NULL
TMP <- NULL
} else { # similar with one day less for non-oddyears
}
This is what I get at the end for TMP3:
data date
TMP2 Character,366 Numeric,366
TMP2 Character,365 Numeric,365
TMP2 Character,365 Numeric,365
What I want is this:
data date
-22.2 1960-01-01
-22.4 1960-02-01
...
Cheers, Eric
Solução
Eric, I agree with Justin, that it is very difficult to give you right answer without reproducible sample. However, I would advice to use xts package when timeseries are involved.
require('xts')
result<-xts(12.22,order.by=as.POSIXct(paste('1990','-','01','-','01',sep='')))
For concatenation use following:
result<-rbind(result,xts(11.22,order.by=as.POSIXct(paste('1991','-','01','-','01',sep=''))))
You should get:
1990-01-01 12.22
1991-01-01 11.22
Outras dicas
It works. And I even found the command I intended to use (unlist()).
result <- xts(as.numeric(unlist(XX[,1])), order.by=as.Date(unlist(XX[,2])))
The entries in my result (XX) were lists inside the matrix. By using unlist() in combination with xts() it works beautifully.
Thanks a lot guys.
Eric