将矩阵中的多个列表对象转换为实际列表
我提取了 45 年时间范围内的每日温度数据。我使用了我能想到的所有可能性,将这些数据追加、cbind、rbind 到一个每年循环增长的变量中。 除了值之外,我还包括时间(as.Date)。不幸的是,我的最终变量由 45 行和 2 列组成,每个单元格包含值(左列)和日期(右列)。但是,我找不到一种方法来“扩展”单元格以获得每个单元格包含一个值的两列。
起初,我尝试使用 merge(, by='date') 从循环中获取更好的变量,但它不起作用,因为我只有一年的日期。 我正在寻找一个命令,它可以扩展单元格,或者更好的是,它实际上会将循环内变量底部的两列(值和日期)作为每个单元格的单个条目附加。
输入数据是一个文本文件,年份块(前导行写有年份,例如 1960 年,以及 31 x 13 矩阵)写在彼此下方。 -9999 是我的 NA 标志。
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
代码简化了:
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
}
这就是我在 TMP3 的最后得到的:
data date
TMP2 Character,366 Numeric,366
TMP2 Character,365 Numeric,365
TMP2 Character,365 Numeric,365
我想要的是这样的:
data date
-22.2 1960-01-01
-22.4 1960-02-01
...
干杯, 埃里克
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
埃里克,
我同意贾斯汀的观点,如果没有可重复的样本,很难给你正确的答案。但是,我建议在涉及时间序列时使用 xts 包。
对于串联使用以下内容:
您应该得到:
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.
For concatenation use following:
You should get:
有用。我什至找到了我打算使用的命令(unlist())。
我的结果 (XX) 中的条目是矩阵内的列表。通过将 unlist() 与 xts() 结合使用,它可以完美地工作。
非常感谢大家。
埃里克
It works. And I even found the command I intended to use (unlist()).
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