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
______________________________________________
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.