[R] List or matrix of object

David Winsemius dwinsemius at comcast.net
Tue Oct 12 18:00:18 CEST 2010


On Oct 12, 2010, at 11:17 AM, Filoche wrote:

>
> Hi everyone.
>
> Is it possible in R to create a matrix or a list (vector) or R  
> object. For
> instance, I have
>
> f1 <- function(x) sqrt(x%*%x);
> f2 <- function(x) (2x+1);
>
> I would like to do something like
>
> L <- List();
> L[1] = f1;
> L[2] = f2;

You should learn a few things  (These should have been explained and  
illustrated as you worked your way through the "An Introduction to R"):

http://cran.r-project.org/doc/manuals/R-intro.pdf

... R is case sensitive so list != List. Changing "List" to "list"  
would help.

... except for the fact that 2x is not a valid expression. Need 2*x

...  and, the "[<-" and "[[<-" operations are different. The use of  
"[[<-" works:

 > f1 <- function(x) sqrt(x%*%x)
 > f2 <- function(x) (2*x+1)
 > L <- list()
 > L[[1]] <- f1
 > L[[2]] <- f2;
 > L

If you want to use "[<-", you will need to give it a valid list object:

 > f1 <- function(x) sqrt(x%*%x)
 > f2 <- function(x) (2*x+1)
 > L <- list()
 > L[1] <- list(f1)
 > L[2] <- list(f2)
 > L

... and drop the use ;'s at the end of lines.

-- 


>
> So, is there a way to create matrix or vector that can contains R  
> object.
>
> With regards,
> Phil
> -- 
> View this message in context: http://r.789695.n4.nabble.com/List-or-matrix-of-object-tp2992101p2992101.html
> Sent from the R help mailing list archive at Nabble.com.

> .

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list