Skip to content

concatenating lists

3 messages · Faheem Mitha, Jan Goebel, Matthew Wiener

#
Dear People,

I have been trying to perform concatenation operations on lists without
success. Consider the following example.
I want to have a function foo such that foo(xy,z) is the same as xyz. This
appears not to be as easy as I thought, though there must be a way.
For example list(xy,z) gives
[[1]]
[[1]][[1]]
[1] 0.6375946 0.1863654

[[1]][[2]]
[1] 0.7395102 0.7528323


[[2]]
[1] 0.5260601 0.8470781

and c(xy,z) gives
[[1]]
[1] 0.6375946 0.1863654

[[2]]
[1] 0.7395102 0.7528323

[[3]]
[1] 0.5260601

[[4]]
[1] 0.847078
but
[[1]]
[1] 0.6375946 0.1863654

[[2]]
[1] 0.7395102 0.7528323

[[3]]
[1] 0.5260601 0.8470781

Unlisting things doesn't seem to help either. I have been fiddling around
for a bit, and have done everything I can think of. Any suggestions?
Thanks in advance.

                             Sincerely, Faheem Mitha.

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Hello Faheem,
On Wed, 14 Mar 2001, Faheem Mitha wrote:

            
Maybe this can give you a starting point.
I guess their are more elegant ways to do this ...
+ 
+   tmp <- paste("list(",
+                paste(paste("liste[[",1:length(liste),"]]",sep=""),
+                      collapse=","),",x)",sep="") 
+   eval(parse(text=tmp))  
+ }
[[1]]
[1] 0.6855152 0.5541156

[[2]]
[1] 0.19300487 0.07686829

[[3]]
[1] 0.4915225 0.9025518

best

jan
#
Here's another possibility:

foo <- function(x,y){
	if(is.list(x)) x <- as.list(x)
	else x <- list(x)

	if(is.list(y)) y <- as.list(y)
	else y <- list(y)

	c(x,y)
}

The if - else clause unnests a nested list or turns a single vector into a
list.  Leaving either one out causes trouble in this example.  

I've checked that this code also successfully combines xyz with another
vector w. 


Now a follow-up question.  What I'd really like to know how to do (and
haven't been able to figure out from the documentation) is how to write an
equivalent version of this function that takes an arbitrary number of
arguments.  I've looked at the code for (for example) sum, which takes
"..." as an argument, but it just refers to internal code. 

Is there an easy way to do this in R code?  Have I missed some of the
documentation that explains this?

Thanks for any help,

Matt Wiener
On Thu, 15 Mar 2001, Jan Goebel wrote:

            
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._