s-news
[Top] [All Lists]

SUMMARY: arguments for list()

To: s-news@wubios.wustl.edu
Subject: SUMMARY: arguments for list()
From: P.J.Wells@open.ac.uk
Date: Fri, 31 May 2002 16:17:59 +0100
Last week I asked how one could save oneself the effort of typing out a long
series of arguments to list(), when the arguments were in a standardised
format, e.g.:

foo.list <- list(foo.1, foo.2, .... foo.n)

where n is a large number (=100 in my case)

Many thanks to those who responded: all were variants on the use of either
get(), or eval(parse()); I append the meat of each reply, both to recognise
those who helped, and because there is probably some instructional value in
seeing the different ways these two basic methods can be implemented.


# using get()


Sundar Dorai-Raj
============

foo.1 <- 1:3
foo.2 <- 4:6
foo.3 <- 7:9
foo.names <- c("foo.1","foo.2","foo.3")
foo.list <- lapply(foo.names,get)

foo.list
[[1]]:
[1] 1 2 3

[[2]]:
[1] 4 5 6

[[3]]:
[1] 7 


Nick Ellis
==========

foo.list <- lapply(paste("foo",1:n,sep="."), get)


Leonid Gibiansky
============

foo.1 <- 1
foo.2 <- 2
foo.3 <- 3
n <- 3
rr <- vector("list",n)
for (i in 1:n){
        rr[[i]] <- get(paste("foo",i,sep=".") )
}
rr


Bert Gunter
=========

You can do it by building up the character expression for the list call
using paste and then eval(parse( ...))) it, starting with :
paste("z",1:100,sep='',collapse=',')

Alternatively, use a simple loop:

foo.list<-vector("list",100)
for(i in 1:100)foo[[i]] <-get(paste('foo',i,sep='.'))


James Holtman
===========

foo.list <- list()
for (i in ls(pattern='foo.')) foo.list[[i]] <- get(i)


Tony Plate
========

lapply(paste("foo",1:n,sep="."), get)


Joseph S. Verducci
=============

for (i in 1:5){
        assign(paste("foo",i,sep="."),seq(i)) }
foo.5

foo.list _ vector(mode="list",length=5)

for(i in 1:5){
        foo.list[[i]] _ get(paste("foo",i,sep="."))}
foo.list


# using eval(parse())


John Fox
=======

Try something like

         lapply(objects(regexpr='foo.?'), function(x) eval(parse(text=x)))

(The syntax for objects() works a bit differently in different version of 
S-PLUS, but your question implies that you already have the character 
vector of names.)



Tom Downing
==========

assuming n=7:

n <- 7
eval ( parse(text=paste("foo.list <- list(",paste("foo.",1:n, sep = "",
collapse = ","),")")    ) )


Yuelin Li
======

Assuming that you already have foo.n created, then perhaps the 
following command would help:

t1 <- paste("foo.", 1:n, collapse=", ", sep="")
eval(parse(text = paste("foo.list <- list(", t1, ")") ))


Dimitris C. Rizopoulos
===============

foo.1 <- 1:10
foo.2 <- 11:20
foo.3 <- 21:30
foo.4 <- 31:40
foo.5 <- 41:50
lapply(1:5, function(x) eval(parse(text=paste("foo.", x, sep=""))))


Sam Buttrey
===========

eval(parse(text = paste("list (", paste ("foo.", 1:n, sep="", collapse=","),
")")))


Julian Wells
OU Business School
The Open University
Walton Hall
Milton Keynes
MK7 6AA
United Kingdom
+44 1908 654658


<Prev in Thread] Current Thread [Next in Thread>
  • SUMMARY: arguments for list(), P . J . Wells <=