2016-08-06 32 views
0

Ich habe einen Datenrahmen wie unten (Namen 1. Reihe Spalte, 2. Reihe Datenelemente) gezeigteine verschachtelte Liste erstellen Obect

Von

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 

Ich versuche, die Datenelemente in der zweiten Reihe zu konvertieren (Flare, Analytik ....) in eine verschachtelte Liste, wie unten

gezeigt
> 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 

Meine Versuche bisher sehr elementar sind und spiegelt meinen Kampf :).

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

Jede Hilfe bei der Lösung dieses Puzzles wird sehr geschätzt. Danke im Voraus.


Im Folgenden finden Sie die Ausgabe von 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") 
+0

Was Sie bisher versucht ? – Rentrop

+0

@ Floo0, meine Frage aktualisiert, 'x = list (name = test1 $ Col_Name, children = c (test1 $ Col_Child_1)' Ich wünschte fast, ich hätte nichts versucht. –

+0

Kannst du die Ausgabe von 'dput (test1)' hinzufügen um den Datenrahmen zu machen – TomNash

Antwort

0

der Code unten nicht gut verallgemeinern, so watch out und viel Glück mit ihm :)

## 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))