R-合并数据“列表”到一个数据帧中

发布于 2024-12-27 18:50:23 字数 2656 浏览 3 评论 0原文

当我使用列表函数时:

el_nino_1974_2000_all <- list()
for (k in seq_along(el_nino_start_month)){
     el_nino_1974_2000_all[[k]] = window(Nino3.4_Flow_1974_2000_zoo,
                                         start = (as.Date(el_nino_1974_2000[k,]$el_nino_start_mont)),
                                         end = (as.Date(el_nino_1974_2000[k,]$el_nino_finish_month)))
}

A 给出了一系列从 i = 1 开始的单独数据子集。但是,我想将所有子集合并为动物园格式或数据帧格式的一帧数据。

这是el_nino_1974_2000_all的结构。

    > str(el_nino_1974_2000_all)
List of 7
 $ :‘zoo’ series from 1976-08-15 to 1977-01-15
  Data: num [1:6, 1:2] 0.519 0.874 0.886 0.823 0.734 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:6], format: "1976-08-15" "1976-09-15" ...
 $ :‘zoo’ series from 1982-05-15 to 1983-06-15
  Data: num [1:14, 1:2] 0.961 1.388 0.959 1.171 1.564 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:14], format: "1982-05-15" "1982-06-15" ...
 $ :‘zoo’ series from 1986-09-15 to 1988-01-15
  Data: num [1:17, 1:2] 0.974 1.089 1.322 1.273 1.313 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:17], format: "1986-09-15" "1986-10-15" ...
 $ :‘zoo’ series from 1991-05-15 to 1992-07-15
  Data: num [1:15, 1:2] 0.68 1 0.923 0.773 0.68 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:15], format: "1991-05-15" "1991-06-15" ...
 $ :‘zoo’ series from 1993-02-15 to 1993-07-15
  Data: num [1:6, 1:2] 0.54 0.641 1.01 1.144 0.917 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:6], format: "1993-02-15" "1993-03-15" ...
 $ :‘zoo’ series from 1994-08-15 to 1995-02-15
  Data: num [1:7, 1:2] 0.662 0.746 1.039 1.329 1.301 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:7], format: "1994-08-15" "1994-09-15" ...
 $ :‘zoo’ series from 1997-04-15 to 1998-05-15
  Data: num [1:14, 1:2] 0.601 1.136 1.461 1.668 2.079 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:14], format: "1997-04-15" "1997-05-15" ...
> 

抱歉,我不知道如何进行格式化。

When I use the list function:

el_nino_1974_2000_all <- list()
for (k in seq_along(el_nino_start_month)){
     el_nino_1974_2000_all[[k]] = window(Nino3.4_Flow_1974_2000_zoo,
                                         start = (as.Date(el_nino_1974_2000[k,]$el_nino_start_mont)),
                                         end = (as.Date(el_nino_1974_2000[k,]$el_nino_finish_month)))
}

A gives a series of separate data subsets staring from i = 1. However, I want to merge all subsets into one frame of data either in zoo format or data frame format.

This is the structure of el_nino_1974_2000_all.

    > str(el_nino_1974_2000_all)
List of 7
 $ :‘zoo’ series from 1976-08-15 to 1977-01-15
  Data: num [1:6, 1:2] 0.519 0.874 0.886 0.823 0.734 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:6], format: "1976-08-15" "1976-09-15" ...
 $ :‘zoo’ series from 1982-05-15 to 1983-06-15
  Data: num [1:14, 1:2] 0.961 1.388 0.959 1.171 1.564 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:14], format: "1982-05-15" "1982-06-15" ...
 $ :‘zoo’ series from 1986-09-15 to 1988-01-15
  Data: num [1:17, 1:2] 0.974 1.089 1.322 1.273 1.313 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:17], format: "1986-09-15" "1986-10-15" ...
 $ :‘zoo’ series from 1991-05-15 to 1992-07-15
  Data: num [1:15, 1:2] 0.68 1 0.923 0.773 0.68 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:15], format: "1991-05-15" "1991-06-15" ...
 $ :‘zoo’ series from 1993-02-15 to 1993-07-15
  Data: num [1:6, 1:2] 0.54 0.641 1.01 1.144 0.917 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:6], format: "1993-02-15" "1993-03-15" ...
 $ :‘zoo’ series from 1994-08-15 to 1995-02-15
  Data: num [1:7, 1:2] 0.662 0.746 1.039 1.329 1.301 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:7], format: "1994-08-15" "1994-09-15" ...
 $ :‘zoo’ series from 1997-04-15 to 1998-05-15
  Data: num [1:14, 1:2] 0.601 1.136 1.461 1.668 2.079 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr [1:2] "Nino3.4_degree_1974_2000" "Houlgrave_flow_1974_2000"
  Index:  Date[1:14], format: "1997-04-15" "1997-05-15" ...
> 

Sorry, I don't know how to do the formatting.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦里的微风 2025-01-03 18:50:23

如果日期不重叠,您可以使用 rbind 将它们粘在一起(因为每个组件的列数相同)。尝试:

el_nino_1974_2000_all <- c()
for (k in seq_along(el_nino_start_month)){
    el_nino_1974_2000_all <- rbind(el_nino_1974_2000_all,window(...))
}

而不是您最初使用的 list 结构。
这将返回一个 zoo 对象。

如果您想返回 data.frame,请尝试使用 rbind,但使用 data.frame 来转换您的对象(即使每个数据集之间的日期索引重叠):

el_nino_1974_2000_all <- data.frame()
for (k in seq_along(el_nino_start_month)){
    el_nino_1974_2000_all <- rbind(el_nino_1974_2000_all,data.frame(window(...)))
}

If the dates don't overlap, you can stick these together using rbind (since the number of columns is the same for each component). Try:

el_nino_1974_2000_all <- c()
for (k in seq_along(el_nino_start_month)){
    el_nino_1974_2000_all <- rbind(el_nino_1974_2000_all,window(...))
}

Instead of the list construction you originally had.
This will return a zoo object.

If you want to return a data.frame, try using rbind, but with data.frame to convert your objects (this will work even if date indices overlap between each of your datasets):

el_nino_1974_2000_all <- data.frame()
for (k in seq_along(el_nino_start_month)){
    el_nino_1974_2000_all <- rbind(el_nino_1974_2000_all,data.frame(window(...)))
}
白日梦 2025-01-03 18:50:23

您是否尝试过此功能:

http://rss.acs.unt .edu/Rdoc/library/gtools/html/smartbind.html

out <- smartbind(list_of_dataframes)

注意:list_of_dataframes 应包含 data.frames,但您可以将数据转换为 dframes即时然后使用此功能。

Have you tried this function :

http://rss.acs.unt.edu/Rdoc/library/gtools/html/smartbind.html

out <- smartbind(list_of_dataframes)

Note : list_of_dataframes should contain data.frames but you can just transform you're data to dframes on the fly and then use this function.

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