将数据集转换为数据框

编程语言 2026-07-10

我有这个数据集

df<-
structure(list(x = list(structure(c(1.2956002895122, 0.84856971403248, 
0.318587376385379, 1.52311609250527, 0.761866777256873, 1.34942921531382, 
1.32182157459345, 0.164293091865351, 1.33391849692185, 1.1741444035705
), dim = c(5L, 2L), dimnames = list(NULL, c("Consumption", "Income"
))), structure(c(0.84804804783009, 0.454659301099081, -0.785771762432506, 
0.746346341726964, 1.07546066123554, 2.36782435778472, 0.33145722604832, 
-1.16704208085918, -0.505189833907774, -0.0305510764003426), dim = c(5L, 
2L), dimnames = list(NULL, c("Consumption", "Income"))), structure(c(1.67206824624292, 
0.621301929142792, -2.64748116335658, 1.06382925864556, 0.481820198662883, 
0.977532978223718, 0.12858898496739, -0.929583796669093, 1.03055091162915, 
1.75054296893639), dim = c(5L, 2L), dimnames = list(NULL, c("Consumption", 
"Income"))), structure(c(1.06124884050636, 1.5695107316969, -0.71012903796485, 
0.697670432455871, 0.441910553257345, 1.131314331505, 1.58797564122333, 
-0.0245687154568635, 1.26341241328479, -0.701603888772303), dim = c(5L, 
2L), dimnames = list(NULL, c("Consumption", "Income"))), structure(c(-0.320387157182086, 
0.909612038039385, -0.30534801455308, 0.13028454605271, 1.04548318778476, 
0.744569146062672, 0.157712771975432, -0.47140382117885, -0.757295165153137, 
1.30319365700164), dim = c(5L, 2L), dimnames = list(NULL, c("Consumption", 
"Income"))), structure(c(1.1328615935615, 1.18848732447009, -1.19259137763868, 
1.61383333543976, 0.112684975627695, -0.294284500868068, 0.653900926135023, 
0.0179116410157527, 2.3485206187256, 0.561897344846424), dim = c(5L, 
2L), dimnames = list(NULL, c("Consumption", "Income"))), structure(c(0.97523495340453, 
2.13698353093441, 0.766663720261808, 0.693029332670238, 0.704645561153127, 
1.29154523355534, 4.14154953485982, -0.454659021170493, 0.55475024788991, 
0.63410937974327), dim = c(5L, 2L), dimnames = list(NULL, c("Consumption", 
"Income"))))), row.names = c(NA, -7L), class = "data.frame")

我该如何把它放到数据框中?现在每一行被视为一个值,无法访问每一行的元素。

解决方案

你的 df 有列表列,其中每一行包含一个矩阵。所以你需要把它们解包。下面给出一种能得到整洁数据框的方法:

do.call(rbind, lapply(seq_along(df$x), function(i) {
  data.frame(group = i, df$x[[i]])
}))

#>    group Consumption      Income
#> 1      1   1.2956003  1.34942922
#> 2      1   0.8485697  1.32182157
#> 3      1   0.3185874  0.16429309
#> 4      1   1.5231161  1.33391850
#> 5      1   0.7618668  1.17414440
#> ...
#> 30     6   0.1126850  0.56189734
#> 31     7   0.9752350  1.29154523
#> 32     7   2.1369835  4.14154953
#> 33     7   0.7666637 -0.45465902
#> 34     7   0.6930293  0.55475025
#> 35     7   0.7046456  0.63410938

创建于2026-04-03,使用 reprex v2.1.1

或使用 {dplyr}

library(dplyr)

df |>
  mutate(group = row_number()) |>
  reframe(
    group = group,
    as.data.frame(x),
    .by = group
  )

如果你想把它以宽格式展示:

library(tidyverse)

df |>
  mutate(group = row_number(), .before = 1,
         x = map(x, ~set_names(c(.x), 
                               paste0(rep(colnames(.x), each = nrow(.x)), "_", 
                                      rep(seq_len(nrow(.x)), ncol(.x)))))) |>
  unnest_wider(x)
#> # A tibble: 7 × 11
#>   group Consumption_1 Consumption_2 Consumption_3 Consumption_4 Consumption_5
#>   <int>         <dbl>         <dbl>         <dbl>         <dbl>         <dbl>
#> 1     1         1.30          0.849         0.319         1.52          0.762
#> 2     2         0.848         0.455        -0.786         0.746         1.08 
#> 3     3         1.67          0.621        -2.65          1.06          0.482
#> 4     4         1.06          1.57         -0.710         0.698         0.442
#> 5     5        -0.320         0.910        -0.305         0.130         1.05 
#> 6     6         1.13          1.19         -1.19          1.61          0.113
#> 7     7         0.975         2.14          0.767         0.693         0.705
#> # ℹ 5 more variables: Income_1 <dbl>, Income_2 <dbl>, Income_3 <dbl>,
#> #   Income_4 <dbl>, Income_5 <dbl>

创建于2026-04-03,使用 reprex v2.1.1

或者在 {base}

data.frame(
  group = seq_len(nrow(df)),
  do.call(rbind, lapply(df$x, function(m) {
    setNames(
      c(m), 
      paste0(rep(colnames(m), each = nrow(m)), "_", 
             rep(seq_len(nrow(m)), ncol(m)))
    )
  }))
)
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章