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.stev...@usu.edu




        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org 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.

Reply via email to