R 如何将嵌套在TIBLE中的列表转换为单个TIBLE,每个列表项位于新行上?
我有一个具有此结构的TIBLE(R 如何将嵌套在TIBLE中的列表转换为单个TIBLE,每个列表项位于新行上?,r,tidyverse,R,Tidyverse,我有一个具有此结构的TIBLE(dput如下): 虽然这通常是tidyr中的unnest()将帮助解决的问题类型,但是CC变量是一个矩阵,而不是一个data.frame(或tibble)。这导致了一些问题。一旦您将矩阵转换为data.frame,我想您会发现unnest()是一个go-to工具 然后,我的第一步是通过CC中的矩阵循环,并使用mutate()中的purr::map循环将它们转换为data.frames。然后我使用unest()一次取消两个变量的测试 以下是它在1.0.0版发布后的工
dput
如下):
虽然这通常是tidyr中的
unnest()
将帮助解决的问题类型,但是CC
变量是一个矩阵,而不是一个data.frame(或tibble)。这导致了一些问题。一旦您将矩阵转换为data.frame,我想您会发现unnest()
是一个go-to工具
然后,我的第一步是通过CC
中的矩阵循环,并使用mutate()
中的purr::map循环将它们转换为data.frames。然后我使用unest()
一次取消两个变量的测试
以下是它在1.0.0版发布后的工作原理
library(tidyr)
library(purrr)
library(dplyr)
dat %>%
mutate(CC = map(CC, as.data.frame) ) %>%
unnest(cols = c(CC, Variance_Type) )
# A tibble: 56 x 4
Date Coefficient ` % Total` Variance_Type
<fct> <dbl> <dbl> <chr>
1 2-17 0.0292 2.92 "Unique to Jsc "
2 2-17 0.180 18.0 "Unique to Uoc "
3 2-17 0.106 10.6 "Unique to FF "
4 2-17 0.356 35.6 "Common to Jsc, and Uoc "
5 2-17 0.0227 2.27 "Common to Jsc, and FF "
6 2-17 0.0471 4.71 "Common to Uoc, and FF "
7 2-17 0.259 25.9 Common to Jsc, Uoc, and FF
8 2-17 1 100 "Total "
9 2-18 0.0414 4.14 "Unique to Jsc "
10 2-18 0.231 23.1 "Unique to Uoc "
下面是另一个基于
数据的解决方案。表包:
library(data.table)
df2 <- setDT(df)[, as.data.table(.SD$CC[[1]], .SD[["Variance_Type"]]), Date]
setnames(df2, "rn", "Variance_Type")
df2
# Date Variance_Type Coefficient % Total
# 1: 2-17 Unique to Jsc 0.0292 2.92
# 2: 2-17 Unique to Uoc 0.1803 18.03
# 3: 2-17 Unique to FF 0.1063 10.63
# 4: 2-17 Common to Jsc, and Uoc 0.3555 35.55
# 5: 2-17 Common to Jsc, and FF 0.0227 2.27
# 6: 2-17 Common to Uoc, and FF 0.0471 4.71
# 7: 2-17 Common to Jsc, Uoc, and FF 0.2588 25.88
# 8: 2-17 Total 1.0000 100.00
# 9: 2-18 Unique to Jsc 0.0414 4.14
# 10: 2-18 Unique to Uoc 0.2307 23.07
# 11: 2-18 Unique to FF 0.1813 18.13
# 12: 2-18 Common to Jsc, and Uoc 0.3428 34.28
# 13: 2-18 Common to Jsc, and FF 0.0408 4.08
# 14: 2-18 Common to Uoc, and FF 0.0137 1.37
# 15: 2-18 Common to Jsc, Uoc, and FF 0.1494 14.94
# 16: 2-18 Total 1.0000 100.00
# 17: 2-19 Unique to Jsc 0.0348 3.48
# 18: 2-19 Unique to Uoc 0.2298 22.98
# 19: 2-19 Unique to FF 0.4534 45.34
# 20: 2-19 Common to Jsc, and Uoc 0.0453 4.53
# 21: 2-19 Common to Jsc, and FF 0.1252 12.52
# 22: 2-19 Common to Uoc, and FF 0.0406 4.06
# 23: 2-19 Common to Jsc, Uoc, and FF 0.0709 7.09
# ....
库(data.table)
带有unnest((cols=c(CC,方差类型))的df2
I getError:cols列的长度必须为7(行数)或者一个,不是14
。但是如果我删除cols
参数,它工作得很好。我会再检查一遍。你的tidyr版本是最新的吗?啊,我有tidyr 0.8.3,所以实际上没有。我正在用Conda管理软件包,所以我想我落后了。哦,一定是这样。tidyr版本1.0.0中没有任何更改。
library(tidyr)
library(purrr)
library(dplyr)
dat %>%
mutate(CC = map(CC, as.data.frame) ) %>%
unnest(cols = c(CC, Variance_Type) )
# A tibble: 56 x 4
Date Coefficient ` % Total` Variance_Type
<fct> <dbl> <dbl> <chr>
1 2-17 0.0292 2.92 "Unique to Jsc "
2 2-17 0.180 18.0 "Unique to Uoc "
3 2-17 0.106 10.6 "Unique to FF "
4 2-17 0.356 35.6 "Common to Jsc, and Uoc "
5 2-17 0.0227 2.27 "Common to Jsc, and FF "
6 2-17 0.0471 4.71 "Common to Uoc, and FF "
7 2-17 0.259 25.9 Common to Jsc, Uoc, and FF
8 2-17 1 100 "Total "
9 2-18 0.0414 4.14 "Unique to Jsc "
10 2-18 0.231 23.1 "Unique to Uoc "
dat %>%
mutate(CC = map(CC, as.data.frame) ) %>%
unnest(CC, Variance_Type )
library(data.table)
df2 <- setDT(df)[, as.data.table(.SD$CC[[1]], .SD[["Variance_Type"]]), Date]
setnames(df2, "rn", "Variance_Type")
df2
# Date Variance_Type Coefficient % Total
# 1: 2-17 Unique to Jsc 0.0292 2.92
# 2: 2-17 Unique to Uoc 0.1803 18.03
# 3: 2-17 Unique to FF 0.1063 10.63
# 4: 2-17 Common to Jsc, and Uoc 0.3555 35.55
# 5: 2-17 Common to Jsc, and FF 0.0227 2.27
# 6: 2-17 Common to Uoc, and FF 0.0471 4.71
# 7: 2-17 Common to Jsc, Uoc, and FF 0.2588 25.88
# 8: 2-17 Total 1.0000 100.00
# 9: 2-18 Unique to Jsc 0.0414 4.14
# 10: 2-18 Unique to Uoc 0.2307 23.07
# 11: 2-18 Unique to FF 0.1813 18.13
# 12: 2-18 Common to Jsc, and Uoc 0.3428 34.28
# 13: 2-18 Common to Jsc, and FF 0.0408 4.08
# 14: 2-18 Common to Uoc, and FF 0.0137 1.37
# 15: 2-18 Common to Jsc, Uoc, and FF 0.1494 14.94
# 16: 2-18 Total 1.0000 100.00
# 17: 2-19 Unique to Jsc 0.0348 3.48
# 18: 2-19 Unique to Uoc 0.2298 22.98
# 19: 2-19 Unique to FF 0.4534 45.34
# 20: 2-19 Common to Jsc, and Uoc 0.0453 4.53
# 21: 2-19 Common to Jsc, and FF 0.1252 12.52
# 22: 2-19 Common to Uoc, and FF 0.0406 4.06
# 23: 2-19 Common to Jsc, Uoc, and FF 0.0709 7.09
# ....