将矩阵中的多个列表对象转换为实际列表

发布于 2025-01-06 14:13:32 字数 2117 浏览 0 评论 0原文

我提取了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花心好男孩 2025-01-13 14:13:32

埃里克,
我同意贾斯汀的观点,如果没有可重复的样本,很难给你正确的答案。但是,我建议在涉及时间序列时使用 xts 包。

require('xts')
result<-xts(12.22,order.by=as.POSIXct(paste('1990','-','01','-','01',sep='')))

对于串联使用以下内容:

result<-rbind(result,xts(11.22,order.by=as.POSIXct(paste('1991','-','01','-','01',sep=''))))

您应该得到:

1990-01-01 12.22
1991-01-01 11.22

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
浪菊怪哟 2025-01-13 14:13:32

有用。我什至找到了我打算使用的命令(unlist())。

result <- xts(as.numeric(unlist(XX[,1])), order.by=as.Date(unlist(XX[,2])))

我的结果 (XX) 中的条目是矩阵内的列表。通过将 unlist() 与 xts() 结合使用,它可以完美地工作。

非常感谢大家。

埃里克

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文