r-将单元格中的列表取消列出到单独的列中-错误



我使用了函数acf按组自动更正:

group_by(filterindex) %>%
summarise(ac = list(acf(meanValence, lag.max = 10)))

我的结果是一个具有2列(filterindex和ac(和10行的DF。

第二列,";ac";在每个单元格中包含";长度为6"的列表;。

我想unlist()这些单元格,这样我就可以按每组构建数据图。

换句话说,我想将列表中的数据提取到列中。

当我尝试使用unlist()时,我得到一个错误:

错误:分配的数据unlist(lagCheck$ac)必须与现有数据兼容。x现有数据有10行。x分配的数据有250行。i只有大小为1的矢量被回收

有没有办法将每个单元格的数据提取到saparate列中?

谢谢!

我正在添加文件:

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元素-在list列上使用mappluck作为"acf"元素和unnest循环

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

您也可以执行以下操作:

library(tidyverse)
cbind(dat[1], map_df(dat$ac, ~setNames(c(.x$acf), .x$lag)))
filterindex 0         1         2         3         4         5         6         7         8         9        10
1    IY1234_11 1 0.8117986 0.6273861 0.5254984 0.4645214 0.4149900 0.3653579 0.3170023 0.2776288 0.2521104 0.2308570
2    IY1234_14 1 0.8129550 0.6609724 0.6062004 0.5669737 0.5286954 0.4970329 0.4752774 0.4516347 0.4280706 0.4109719
3     IY1234_2 1 0.7941270 0.6229119 0.5260023 0.4579149 0.3967963 0.3504375 0.3134361 0.2874294 0.2615010 0.2296035
4     IY1234_5 1 0.8621462 0.7367429 0.6591466 0.5985105 0.5519736 0.5122406 0.4718836 0.4377369 0.4098755 0.3848915
5     IY1234_9 1 0.8325796 0.6960175 0.6194843 0.5644403 0.5190581 0.4827734 0.4488699 0.4073324 0.3664055 0.3275778
6    YF1234_11 1 0.6855649 0.4548485 0.3502863 0.2688841 0.2390786 0.2101969 0.2000641 0.1928846 0.1845500 0.1712050
7    YF1234_15 1 0.6700960 0.4337252 0.3526648 0.3036932 0.2512038 0.2145188 0.2052665 0.2029319 0.2036108 0.1866808
8     YF1234_3 1 0.5916501 0.3473729 0.2559888 0.2235636 0.1897835 0.1754304 0.1538725 0.1425648 0.1437799 0.1279340
9     YF1234_5 1 0.7118862 0.4689141 0.3759668 0.3261070 0.2842125 0.2544111 0.2366003 0.2152661 0.1904011 0.1585064
10    YF1234_9 1 0.6674309 0.4358518 0.3341741 0.2628173 0.2035307 0.1860288 0.1922537 0.1970012 0.1811446 0.1817791

相关内容

最新更新