1

假设我有一个数据集作为列表对象。这是一种快速生成一些随机数据的方法:

a <- list(x1=rnorm(10),x2=rnorm(10))
b <- list(y1=rnorm(10),y2=rnorm(10),y3=rnorm(10))
c <- list(x1=rnorm(10),x2=rnorm(10))
d <- list(y1=rnorm(10),y2=rnorm(10),y3=rnorm(10))
e <- list(x1=rnorm(10),x2=rnorm(10))
f <- list(y1=rnorm(10),y2=rnorm(10),y3=rnorm(10))
branch1 <- list(a,b)
branch2 <- list(c,d)
branch3 <- list(e,f)
data <- list(branch1,branch2,branch3)

数据将如下所示:

> data
[[1]]
[[1]][[1]]
[[1]][[1]]$x1
 [1] -0.04944765 -2.52337788  0.30997940 -0.71163967  0.48528227  1.17816655  0.84655561 -2.17030509
 [9] -0.03489831 -0.18292103

[[1]][[1]]$x2
 [1] -0.96112288 -0.57049890 -1.08079903 -3.12189611  0.04508508 -0.38865962  1.61032327 -0.63870639
 [9]  0.77297993  0.48404346


[[1]][[2]]
[[1]][[2]]$y1
 [1]  1.1722426 -0.9313285  2.7288262  1.9429077  0.4842252  1.4551183  0.2364701  0.8246233 -0.8540208
[10]  1.9039492

[[1]][[2]]$y2
 [1]  1.07758478 -0.90898684  1.00826795  0.97841961  0.70665070  0.05041242 -0.88985476  1.46972378
 [9] -2.38069537  1.87440491

[[1]][[2]]$y3
 [1] -0.08308981 -0.37208124  0.70363404 -0.88521094 -0.54751152  0.43910681  0.35753713 -0.83412426
 [9] -1.12528856 -1.61850682



[[2]]
[[2]][[1]]
[[2]][[1]]$x1
 [1]  0.01377682  0.19047533 -0.17129543  0.56909904 -0.50928670  1.11754925  0.43099918 -0.37832047
 [9] -0.56111033  0.73358678

[[2]][[1]]$x2
 [1]  2.3302948 -0.9129171  0.7549973 -1.0379182 -1.9319941 -0.7924752  0.6500014  0.4178630  1.3118695
[10] -1.2121514


[[2]][[2]]
[[2]][[2]]$y1
 [1]  0.8275016  0.7133281 -0.5240551  1.6891841 -0.5621567 -0.7496414  0.5983334  1.3617800 -0.4837031
[10]  0.4461964

[[2]][[2]]$y2
 [1] -0.8155957  2.0451346  0.5882782 -0.4417398 -0.2335832 -1.4218698 -1.3949293  0.6129234 -0.6052243
[10]  0.1414337

[[2]][[2]]$y3
 [1] -0.3374557  0.3310320 -0.8983646  1.7219435  0.2828211  0.1116751  0.6373569  0.3369811 -1.7301219
[10]  0.6329963



[[3]]
[[3]][[1]]
[[3]][[1]]$x1
 [1]  0.8392581 -1.0852638  0.2278769 -0.2653210  0.1231531  0.7964941 -1.0282257  1.0272565  0.6882902
[10]  0.2056550

[[3]][[1]]$x2
 [1]  1.03245491 -0.06994712 -0.53785824 -1.00255262 -0.22105177 -1.07233849  0.93624906  1.03518821
 [9]  1.24065942 -3.51732843


[[3]][[2]]
[[3]][[2]]$y1
 [1] -0.30520118  0.60494774 -1.45178410 -0.32739980 -0.15773973  0.76237976 -0.04449191  0.31864511
 [9] -0.12763899 -0.78701147

[[3]][[2]]$y2
 [1]  0.02523319  1.10718390 -0.24078229  1.44386520  0.52889358  0.04096228 -0.05084876  0.81393090
 [9]  2.14785155 -2.23776686

[[3]][[2]]$y3
 [1]  2.62345162 -0.07802202  0.12745068 -0.92882646 -0.09972968  0.24213258  0.48338620  0.38180029
 [9]  1.09133254 -0.29834029

假设我想从每个列表中提取 y1、y2 和 y3 并形成如下所示的数据框:

 ID  value1 value2 value3
  1      y1     y2     y3
  2      y1     y2     y3
  3      y1     y2     y3
  4      y1     y2     y3
  5      y1     y2     y3
  6      y1     y2     y3
...     ...    ...    ...

我怎么能这样做?

4

2 回答 2

4
ll <- lapply(data, 
          function(X) data.frame(unlist(X, recursive=FALSE)[c("y1","y2","y3")]))
df <- do.call(rbind, ll)

head(df)
#            y1         y2           y3
# 1 -1.19755667  0.5993736 -0.385201447
# 2 -1.58565372  2.1750677 -0.555607218
# 3  2.06039627 -0.4069106 -0.002523344
# 4  1.02220253 -0.8521903 -2.247354246
# 5 -0.05043813  0.2651378  0.114774551
# 6  1.00512768 -0.7002903 -0.539840393

要(部分)了解它是如何工作的,请查看unlist(data[[1]], recursive=FALSE).

于 2013-04-29T21:49:15.597 回答
0

一种极其不雅的方式暗示了您如何使用递归编写它:

extractor <- function(x) {
  if("y1" %in% names(x)) {
    x[grepl("y",names(x))]
  } else {
    NULL
  }
}
> as.data.frame(unlist(unlist(lapply(data,function(x) lapply(x,extractor)), recursive=FALSE),recursive=FALSE))
           y1         y2         y3        y1.1       y2.1        y3.1       y1.2       y2.2       y3.2
1   0.5500440  0.8009143  0.8641525  1.37000399 -0.2262220 -1.15591653  0.9411658 -1.9429564  0.8993541
2   1.2366758  1.1652534 -1.1986224  0.67325386  0.3814258 -0.12714861  0.1705881 -0.1163025 -0.8308115
3   0.1390979  0.3588557  0.6394920  0.07216675 -0.7835158 -1.94151838 -1.0634979  1.1393963 -0.3305447
4   0.4102751 -0.6085572  2.4302267 -1.50775732  0.5829914  1.18118089 -1.3880490  0.6361240  0.7408145
5  -0.5584569 -0.2022409 -0.5572155  0.02610023 -1.3165104  1.85991086  2.0867174 -0.4929374  0.9899716
6   0.6053707 -0.2732481  0.8449042 -0.31641587 -2.8097747  1.07401226 -0.6785031 -0.8341882 -1.9385047
7  -0.5063335 -0.4686998 -0.7822018 -0.10234651  0.4649680 -0.02734697 -1.8555717  0.2710668  0.1071904
8  -1.4205655  0.7041673  1.1107114 -1.18155923  0.8405398 -0.03333034  0.5332594  0.1573533  0.6087790
9   0.1279930 -1.1973635  0.2498247  0.49865804 -0.2858454 -1.51606762  0.3102303  0.6297117 -1.4508243
10  1.9458512  0.8663661  1.6519154 -1.03895644  0.5041263  0.79038534 -1.3538343 -0.3957980  0.4806256
于 2013-04-29T21:45:21.213 回答