An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120203/0e6108c3/attachment.pl>
combining data structures
4 messages · David Stevens, Pete Brecknock
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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20120204/76a43032/attachment.pl>
David 1. The last line of the code should have been ... Output = as.data.frame(do.call(rbind,nn)) # Output Node Connect.up Connect.down 1 1 NULL 2, 3 2 2 1 4, 5 3 3 NULL 2, 3 4 4 1 4, 5 Apologies for any confusion 2. I am not familiar with the diagram package or the examples you describe. Why the desire to create a data frame? Why not just use a list? HTH Pete dkStevens wrote
Thanks for the reply. Two things - I must have something missing because copying and pasting your example gave me an error ... your definitions of nn Output = do.call(as.data.frame(rbind),nn) Error in as.data.frame.default(rbind) : cannot coerce class '"function"' into a data.frame The second is the need, and why I'm not sure this is the best way. This is to customize the flow charting library code from the library 'diagram' to use a node-and-link characterization of a network - think PERT charts or perhaps linking river segments in a water quality model. Each Node will be a, say, circle and has attributes of being connected up to one or more upstream nodes and down to one or more downstream nodes. So the Connect.up column is a vector up upstream connections, usually one but sometimes > one, and likewise Connect.down. There is an accompanying table of links with attributes of which nodes are at each end of a link and other metadata that describe the link (e.g. the length of time required to traverse the link, its name etc. That said, my thought was that the situation was too simple to fire up a full-blown object system beyond what R provides natively. I guess it's like making a data frame that has some 3-d elements. On 2/3/2012 9:32 PM, Pete Brecknock wrote:
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)
-- 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-tp4356288p4358435.html Sent from the R help mailing list archive at Nabble.com.