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; 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.
Hi again everyone. I found I could use a list with l = list() l[[1]] = myObj instead of l[1] = myObj Anyone can explain me why the use of double [] is required? Regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/List-or-matrix-of-object-tp2992101p2992121.html Sent from the R help mailing list archive at Nabble.com.
You probably need to review the Intro to R to understand indexing:> f1 <- function(x) sqrt(x%*%x); > f2 <- function(x) (2*x+1); > L <- list() > L[[1]] <- f1 > L[[2]] <- f2 > L # contains the objects[[1]] function (x) sqrt(x %*% x) [[2]] function (x) (2 * x + 1)> L[[1]](3) # now call the functions in the list[,1] [1,] 3> L[[2]](42)[1] 85>On Tue, Oct 12, 2010 at 11:17 AM, Filoche <pmassicotte at hotmail.com> 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; > > 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. > > ______________________________________________ > R-help at 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
It is not clear exactly what you are trying to do, but this works: f1 <- function(x) sqrt(x%*%x) f2 <- function(x) {2*x+1} L <- list(); L[[1]] = f1; L[[2]] = f2; Then you can do something like: L[[2]](5) -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Filoche > Sent: Tuesday, October 12, 2010 9:17 AM > To: r-help at r-project.org > Subject: [R] List or matrix of object > > > 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; > > 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. > > ______________________________________________ > R-help at 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.
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
Thank you everyone for your answers. Regards, Phil -- View this message in context: http://r.789695.n4.nabble.com/List-or-matrix-of-object-tp2992101p2992304.html Sent from the R help mailing list archive at Nabble.com.
The difference is the same as the difference between a set with 1 element and a single element from a set. The single [ extracts/replaces/assigns a subset of the list elements, but the piece is still a list (even if it is one element). So when you are assigning using [ you need to give it a list, not a single object. If you create a list then do something like mode( mylist[1] ) you will see that it is still a list. The double [[ exctracts/replaces/assigns a single element of the list, it does not work on anything more than a single element, but it works with that element as its own object, not a list (unless it is a list). So [[ can be used to assign a single element without needing to create a list (but can only do a single element where [ can do 1 or more). If you do mode( mylist[[1]] ) then you will see that the single element is no longer a list. Hope that helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Filoche > Sent: Tuesday, October 12, 2010 9:29 AM > To: r-help at r-project.org > Subject: Re: [R] List or matrix of object > > > Hi again everyone. > > I found I could use a list with > > l = list() > l[[1]] = myObj > > instead of > > l[1] = myObj > > Anyone can explain me why the use of double [] is required? > > Regards, > Phil > > -- > View this message in context: http://r.789695.n4.nabble.com/List-or- > matrix-of-object-tp2992101p2992121.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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.