我使用了函数 acf
按组进行自动关联:
group_by(filterindex) %>%
summarise(ac = list(acf(meanValence, lag.max = 10)))
我的结果是具有 2 列(filterindex 和 ac)且有 10 行的 DF。
第二列“ac”在每个单元格中包含“长度为 6 的列表”。
我想unlist()
这些单元格,以便我可以按每个组构建数据图。
换句话说,我想将列表中的数据提取到列中。
当我尝试使用 unlist()
时,出现错误:
Error: Assigned data
unlist(lagCheck$ac)
must be compatible with existing data. x Existing data has 10 rows. x Assigned data has 250 rows. i Only vectors of size 1 are recycled
有什么方法可以将每个单元格中的数据提取到单独的列中吗?
谢谢!
我正在添加文件:
structure(list(filterindex = c("IY1234_11", "IY1234_14", "IY1234_2",
"IY1234_5", "IY1234_9", "YF1234_11", "YF1234_15", "YF1234_3",
"YF1234_5", "YF1234_9"), ac = list(structure(list(acf = structure(c(1,
0.811798556550654, 0.627386051181487, 0.525498436917337, 0.464521367964671,
0.414990027612064, 0.365357901454317, 0.317002282870693, 0.277628764819084,
0.252110413675042, 0.230857048035993), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 3559L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"), structure(list(acf = structure(c(1,
0.812954955747791, 0.660972431349436, 0.606200383929671, 0.566973727353185,
0.528695448431704, 0.497032929064658, 0.475277413972852, 0.451634687077036,
0.428070611800892, 0.410971876587924), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 3600L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"), structure(list(acf = structure(c(1,
0.794127006651825, 0.622911924040662, 0.526002295899464, 0.457914856216244,
0.396796288075039, 0.350437514598721, 0.31343606030936, 0.287429425131587,
0.261501017028658, 0.229603463164826), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 3511L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"), structure(list(acf = structure(c(1,
0.86214616647042, 0.736742907476865, 0.659146619768523, 0.598510521420571,
0.551973561891987, 0.512240559662018, 0.471883614972944, 0.437736880271234,
0.409875465478741, 0.384891544522991), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 3368L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"), structure(list(acf = structure(c(1,
0.832579604548681, 0.696017549671124, 0.619484328593781, 0.564440279554403,
0.519058064117993, 0.482773415455994, 0.448869933383051, 0.407332365756958,
0.366405526731125, 0.32757780840695), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 3576L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"), structure(list(acf = structure(c(1,
0.68556492740373, 0.454848510565868, 0.350286285053742, 0.268884136347556,
0.239078585315277, 0.210196854542057, 0.20006410547457, 0.192884602043769,
0.18454996341636, 0.171205027821855), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 3597L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"), structure(list(acf = structure(c(1,
0.670095995265377, 0.43372520047406, 0.352664781684659, 0.303693247694507,
0.251203828506515, 0.214518820918493, 0.205266460850874, 0.20293193447206,
0.203610756614635, 0.186680789521751), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 3205L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"), structure(list(acf = structure(c(1,
0.591650063857695, 0.347372920357488, 0.255988756063866, 0.223563644580496,
0.189783476665092, 0.175430441726183, 0.153872532976106, 0.142564779399524,
0.143779895895664, 0.127934048803667), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 3298L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"), structure(list(acf = structure(c(1,
0.71188624496217, 0.468914143623161, 0.375966807510404, 0.326107000887847,
0.284212517761139, 0.254411081196079, 0.236600345854638, 0.215266086252369,
0.190401107127801, 0.158506374580939), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 2938L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"), structure(list(acf = structure(c(1,
0.667430910491119, 0.435851760081718, 0.334174146985532, 0.262817311918962,
0.20353074131307, 0.186028793933915, 0.192253655643755, 0.197001241058914,
0.181144578826439, 0.181779077174667), .Dim = c(11L, 1L, 1L)),
type = "correlation", n.used = 3598L, lag = structure(c(0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim = c(11L, 1L, 1L)), series = "meanValence",
snames = NULL), class = "acf"))), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
谢谢!
最佳答案
我们可能需要提取 acf
元素 - 使用 map
在 list
列上循环,pluck
“acf”元素和取消嵌套
library(dplyr)
library(purrr)
library(tidyr)
df1 %>%
mutate(ac = map(ac, ~ pluck(.x, "acf") %>%
as.vector)) %>%
unnest(ac)
-输出
# A tibble: 110 × 2
filterindex ac
<chr> <dbl>
1 IY1234_11 1
2 IY1234_11 0.812
3 IY1234_11 0.627
4 IY1234_11 0.525
5 IY1234_11 0.465
6 IY1234_11 0.415
7 IY1234_11 0.365
8 IY1234_11 0.317
9 IY1234_11 0.278
10 IY1234_11 0.252
# … with 100 more rows
如果我们需要单独的列,请使用unnest_wider
library(stringr)
df1 %>%
mutate(ac = map(ac, ~ pluck(.x, "acf") %>%
as.vector %>%
setNames(str_c('ac', seq_along(.))))) %>%
unnest_wider(ac)
-输出
# A tibble: 10 × 12
filterindex ac1 ac2 ac3 ac4 ac5 ac6 ac7 ac8 ac9 ac10 ac11
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 IY1234_11 1 0.812 0.627 0.525 0.465 0.415 0.365 0.317 0.278 0.252 0.231
2 IY1234_14 1 0.813 0.661 0.606 0.567 0.529 0.497 0.475 0.452 0.428 0.411
3 IY1234_2 1 0.794 0.623 0.526 0.458 0.397 0.350 0.313 0.287 0.262 0.230
4 IY1234_5 1 0.862 0.737 0.659 0.599 0.552 0.512 0.472 0.438 0.410 0.385
5 IY1234_9 1 0.833 0.696 0.619 0.564 0.519 0.483 0.449 0.407 0.366 0.328
6 YF1234_11 1 0.686 0.455 0.350 0.269 0.239 0.210 0.200 0.193 0.185 0.171
7 YF1234_15 1 0.670 0.434 0.353 0.304 0.251 0.215 0.205 0.203 0.204 0.187
8 YF1234_3 1 0.592 0.347 0.256 0.224 0.190 0.175 0.154 0.143 0.144 0.128
9 YF1234_5 1 0.712 0.469 0.376 0.326 0.284 0.254 0.237 0.215 0.190 0.159
10 YF1234_9 1 0.667 0.436 0.334 0.263 0.204 0.186 0.192 0.197 0.181 0.182
关于r - 将单元格中的列表取消列出到单独的列中 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70882538/