创建嵌套列表obect

时间:2016-08-06 16:31:31

标签: r list nested nested-lists

我有一个如下所示的数据帧(第一行列名,第二行数据元素)

 Col_Name   Col_Child_1   Col_Grand_Child_1  Col_Great_Grand_Child_1    Col_Great_Grand_Child_Size1    Col_Great_Grand_Child_2    Col_Great_Grand_Child_Size2    Col_Great_Grand_Child_3    Col_Great_Grand_Child_Size3     Col_Great_Grand_Child_4    Col_Great_Grand_Child_Size4                  
 Flare      analytics     cluster            AgglomerativeCluster       3938                           CommunityStructure         3812                           HierarchicalCluster        6714                            MergeEdge                  743

我正在尝试将第二行(Flare,analytics ....)中的数据元素转换为嵌套列表,如下所示

> Flare
$name
[1] "flare"

$children
$children[[1]]
$children[[1]]$name
[1] "analytics"

$children[[1]]$children
$children[[1]]$children[[1]]
$children[[1]]$children[[1]]$name
[1] "cluster"

$children[[1]]$children[[1]]$children
$children[[1]]$children[[1]]$children[[1]]
$children[[1]]$children[[1]]$children[[1]]$name
[1] "AgglomerativeCluster"

$children[[1]]$children[[1]]$children[[1]]$size
[1] 3938


$children[[1]]$children[[1]]$children[[2]]
$children[[1]]$children[[1]]$children[[2]]$name
[1] "CommunityStructure"

$children[[1]]$children[[1]]$children[[2]]$size
[1] 3812


$children[[1]]$children[[1]]$children[[3]]
$children[[1]]$children[[1]]$children[[3]]$name
[1] "HierarchicalCluster"

$children[[1]]$children[[1]]$children[[3]]$size
[1] 6714


$children[[1]]$children[[1]]$children[[4]]
$children[[1]]$children[[1]]$children[[4]]$name
[1] "MergeEdge"

$children[[1]]$children[[1]]$children[[4]]$size
[1] 743

到目前为止,我的尝试非常基本,反映了我的斗争:)。

x = list(name= test1$Col_Name, children = c(test1$Col_Child_1) 

非常感谢任何有关解决这个难题的帮助。提前谢谢。

以下是dput(test1)

的输出
test1 = structure(list(Col_Name = structure(2L, .Names = "row1", .Label = c("Col_Name", 
"Flare"), class = "factor"), Col_Child_1 = structure(1L, .Names = "row1", .Label = c("analytics", 
"Col_Child_1"), class = "factor"), Col_Grand_Child_1 = structure(1L, .Names = "row1", .Label = c("cluster", 
"Col_Grand_Child_1"), class = "factor"), Col_Great_Grand_Child_1 = structure(1L, .Names = "row1", .Label = c("AgglomerativeCluster", 
"Col_Great_Grand_Child_1"), class = "factor"), Col_Great_Grand_Child_Size1 = structure(1L, .Names = "row1", .Label = c("3938", 
"Col_Great_Grand_Child_Size1"), class = "factor"), Col_Great_Grand_Child_2 = structure(2L, .Names = "row1", .Label = c("Col_Great_Grand_Child_2", 
"CommunityStructure"), class = "factor"), Col_Great_Grand_Child_Size2 = structure(1L, .Names = "row1", .Label = c("3812", 
"Col_Great_Grand_Child_Size2"), class = "factor"), Col_Great_Grand_Child_3 = structure(2L, .Names = "row1", .Label = c("Col_Great_Grand_Child_3", 
"HierarchicalCluster"), class = "factor"), Col_Great_Grand_Child_Size3 = structure(1L, .Names = "row1", .Label = c("6714", 
"Col_Great_Grand_Child_Size3"), class = "factor"), Col_Great_Grand_Child_4 = structure(2L, .Names = "row1", .Label = c("Col_Great_Grand_Child_4", 
"MergeEdge"), class = "factor"), Col_Great_Grand_Child_Size4 = structure(1L, .Names = "row1", .Label = c("743", 
"Col_Great_Grand_Child_Size4"), class = "factor")), .Names = c("Col_Name", 
"Col_Child_1", "Col_Grand_Child_1", "Col_Great_Grand_Child_1", 
"Col_Great_Grand_Child_Size1", "Col_Great_Grand_Child_2", "Col_Great_Grand_Child_Size2", 
"Col_Great_Grand_Child_3", "Col_Great_Grand_Child_Size3", "Col_Great_Grand_Child_4", 
"Col_Great_Grand_Child_Size4"), row.names = 2L, class = "data.frame")

1 个答案:

答案 0 :(得分:0)

下面的代码并不能很好地概括,所以请注意并祝你好运:)

## get rid of factors
dat <- data.frame(lapply(dat, as.character), stringsAsFactors=FALSE)

## identify descendants -- hoping that the great grand children do not reproduce further
Children.names <- grep('Col_Child_[[:digit:]]', colnames(dat))
Grand_Children.names <- grep('Col_Grand_Child_[[:digit:]]', colnames(dat))
Great_Grand_Children.names <- grep('Col_Great_Grand_Child_[[:digit:]]', colnames(dat))
Great_Grand_Children.sizes <- grep('Col_Great_Grand_Child_Size[[:digit:]]', colnames(dat))

## putting it together into a list of lists (of lists)
nggc <- length(Great_Grand_Children.sizes)
ggc <- lapply(1:nggc, function(i) list(name=dat[1,Great_Grand_Children.names[i]], size=as.numeric(dat[Great_Grand_Children.sizes[i]])))
gc <- list(name=dat[1,Grand_Children.names[1]], children=ggc)

## fingers crossed now...
ll <- list(name=dat$Col_Name)
ll$children <- list( list(name=dat[1,Children.names[1]], children=gc) )