[R] combining data structures

Pete Brecknock Peter.Brecknock at bp.com
Sat Feb 4 05:32:27 CET 2012


Not entirely sure why you would want a data.frame that has multiple entries
in one of the columns (Connect.down) but leaving that aside is the following
of any use?

nn=list()
    
nn[[1]] =  list(Node = "1", Connect.up = c(NULL), Connect.down = c(2,3))
nn[[2]] =  list(Node = "2", Connect.up = c(1), Connect.down = c(4,5))
nn[[3]] =  list(Node = "3", Connect.up = c(NULL), Connect.down = c(2,3))
nn[[4]] =  list(Node = "4", Connect.up = c(1), Connect.down = c(4,5))

Output = do.call(as.data.frame(rbind),nn)

# Output
  value.Node value.Connect.up value.Connect.down
1          1             NULL               2, 3
2          2                1               4, 5
3          3             NULL               2, 3
4          4                1               4, 5

HTH

Pete



dkStevens wrote
> 
> Group
> 
> It's unlikely I'm trying this the best way, but I'm trying to create a 
> data structure from elements like
> 
> nNode = 2
> nn = vector("list",nNode)
> 
> nn[[1]] =  list(Node = "1", Connect.up = c(NULL), Connect.down = c(2,3))
> nn[[2]] =  list(Node = "2", Connect.up = c(1), Connect.down = c(4,5))
> ....  #( and eventually many more nodes)
> 
> NodeList = as.data.frame(nn[[1]])
> for(i in 2:nNode) {
>    NodeList = rbind(NodeList,as.data.frame(nn[[i]]))
> }
> 
> and is trying to create a data frame with many rows and three columns: 
> Node, Connect.up,Connect.down
> in which the Connect.up and Connect.down columns may be single numbers 
> or vectors of numbers.  The above approach gives an error:
> 
> Error in data.frame(Node = "1", Connect.up = NULL, Connect.down = c(2,  :
>    arguments imply differing number of rows: 1, 0, 2
> 
> My earlier try by brute force worked fine:
> 
> NodeList = as.data.frame(rbind(nn[[1]],nn[[2]]))
> 
>  > NodeList
>    Node Connect.up Connect.down
> 1    1       NULL         2, 3
> 2    2          1         4, 5
> 
> and gives me what I want (many more rows eventually). But I want to do 
> this generically from the problem context in a procedure so I won't know 
> up front how many nodes I'll have.
> 
> Clearly I'm not understanding how referencing works for lists like I've 
> created.  Can anyone shed light on this?
> 
> -- 
> David K Stevens, P.E., Ph.D., Professor
> Civil and Environmental Engineering
> Utah Water Research Laboratory
> 8200 Old Main Hill
> Logan, UT  84322-8200
> 435 797 3229 - voice
> 435 797 1363 - fax
> david.stevens@
> 
> 
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@ mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 


--
View this message in context: http://r.789695.n4.nabble.com/combining-data-structures-tp4356288p4356547.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list