连接具有不同数量元素的嵌套列表

发布于 2024-12-05 17:57:54 字数 1179 浏览 5 评论 0原文

我是 R 的新手,有一个我无法解决的串联问题。

我有一个巨大的数据框,其形式为:

station POSIX date.str forec.time Lead.timemean.ens obs

6019 2011-08-06 06:00 20110806 00 006 45 67

6019 2011-08-06 07:00 20110806 00 007 69 72

6031 2011-08-06 12:00 20110806 06 006 87 95

6031 2011-08-06 13:00 20110806 06 007 88 97

我用过“ply”像这样分割数据框

mydata.split <- dlply(mydataframe, .(datestr), dlply, .(forec.time), dlply, .(lead.time), Identity, .drop = FALSE)

我做了一些用数据进行计算,这需要将数据以这种方式拆分。我将这个新列表称为 mynewlist af 计算。我想连接这些数据,但由于列表元素的数量不同,我遇到了问题。

<代码>>长度(mynewlist[[1]][[1]])

<代码>[1] 34

<代码>>长度(mynewlist[[1]][[2]])

<代码>[1] 38

我尝试使用 do.call( rbind, do.call( rbind, do .call( rbind, mynewlist) ) ) 将列表连接到数据框中,但我收到以下消息:

In function (..., deparse.level = 1) : 结果的列数不是向量长度(arg 1)的倍数

有没有办法连接具有不同数量元素的嵌套列表?

我很高兴获得帮助或指明方向。 看待 西塞

I am a newbie to R and have a concatenation problem that I haven't been able to solve.

I have a huge data frame of the form:

station POSIX date.str forec.time lead.time mean.ens obs

6019 2011-08-06 06:00 20110806 00 006 45 67

6019 2011-08-06 07:00 20110806 00 007 69 72

6031 2011-08-06 12:00 20110806 06 006 87 95

6031 2011-08-06 13:00 20110806 06 007 88 97

I have use "ply" to split the data frame like this

mydata.split <- dlply(mydataframe, .(datestr), dlply, .(forec.time), dlply, .(lead.time), identity, .drop = FALSE)

I do some calculation with data, which require that data are split up this way. I call this new list mynewlist af calculations. I would like to concatenate these data, but I run into problems because of differing number of list elements.

> length(mynewlist[[1]][[1]])

[1] 34

> length(mynewlist[[1]][[2]])

[1] 38

I have tried to use do.call( rbind, do.call( rbind, do.call( rbind, mynewlist) ) ) to concatenate the list into a data frame, but I get the following message:

In function (..., deparse.level = 1) :
number of columns of result is not a multiple of vector length (arg 1)

Is there a way of concatenating a nested list with differing number of elements?

I am greateful for help or a point in a direction.
Regard
Sisse

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

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

发布评论

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

评论(1

帥小哥 2024-12-12 17:57:54

只需使用 ldply 即可将所有这些列表重新拼接在一起。

使用 plyr 中的 baseball 数据,按照问题中的方式使用 dlply 吐出数据:

library(plyr)
x <- dlply(baseball, .(year), transform, mean_rbi = mean(rbi)) 

现在使用 ldply 来将列表合并到一个 data.frame 中:

y <- ldply(x)

结果:

str(y)
'data.frame':   21699 obs. of  23 variables:
 $ id      : chr  "ansonca01" "forceda01" "mathebo01" "startjo01" ...
 $ year    : int  1871 1871 1871 1871 1871 1871 1871 1872 1872 1872 ...
 $ stint   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ team    : chr  "RC1" "WS3" "FW1" "NY2" ...
 $ lg      : chr  "" "" "" "" ...
 $ g       : int  25 32 19 33 29 29 29 46 37 25 ...
 ...
 $ rbi     : int  16 29 10 34 23 21 23 50 15 16 ...
 ...
 $ gidp    : int  NA NA NA NA NA NA NA NA NA NA ...
 $ mean_rbi: num  22.3 22.3 22.3 22.3 22.3 ...

Just use ldply to stitch all those lists back together.

With the baseball data in plyr, use dlply as in your question to spit the data:

library(plyr)
x <- dlply(baseball, .(year), transform, mean_rbi = mean(rbi)) 

Now use ldply to combine the lists into a data.frame:

y <- ldply(x)

The results:

str(y)
'data.frame':   21699 obs. of  23 variables:
 $ id      : chr  "ansonca01" "forceda01" "mathebo01" "startjo01" ...
 $ year    : int  1871 1871 1871 1871 1871 1871 1871 1872 1872 1872 ...
 $ stint   : int  1 1 1 1 1 1 1 1 1 1 ...
 $ team    : chr  "RC1" "WS3" "FW1" "NY2" ...
 $ lg      : chr  "" "" "" "" ...
 $ g       : int  25 32 19 33 29 29 29 46 37 25 ...
 ...
 $ rbi     : int  16 29 10 34 23 21 23 50 15 16 ...
 ...
 $ gidp    : int  NA NA NA NA NA NA NA NA NA NA ...
 $ mean_rbi: num  22.3 22.3 22.3 22.3 22.3 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文