May I ask how to initialize a list? usually I will use " result=list(0) " to do this. is this right? [[alternative HTML version deleted]]
On Mon, Jun 21, 2010 at 4:18 PM, song song <rprojecthelp at gmail.com> wrote:> May I ask how to initialize a list? > > usually I will use " result=list(0) " ?to do this. is this right?It works, but it is cleaner to use results=list() The difference is that list(0) will have one component that contains the number zero, whereas list() will simply initialize an empty list (no components). Peter
On Jun 21, 2010, at 7:18 PM, song song wrote:> May I ask how to initialize a list? > > usually I will use " result=list(0) " to do this. is this right?There is now something in that list, namely an element whose value is zero. If you append to it with the c() function, the 0 will still be there. > result=list(0) > result=c(result, 5) > result [[1]] [1] 0 [[2]] [1] 5 What's wrong with result=list() result=c(result, 5) > result [[1]] [1] 5 David Winsemius, MD West Hartford, CT
Use result <- vector("list", n) to make a list of length n, all of whose components are NULL. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Peter Langfelder > Sent: Monday, June 21, 2010 4:23 PM > To: song song > Cc: r-help at r-project.org > Subject: Re: [R] how to initial a list to store data result? > > On Mon, Jun 21, 2010 at 4:18 PM, song song > <rprojecthelp at gmail.com> wrote: > > May I ask how to initialize a list? > > > > usually I will use " result=list(0) " ?to do this. is this right? > > It works, but it is cleaner to use > > results=list() > > The difference is that list(0) will have one component that contains > the number zero, whereas list() will simply initialize an empty list > (no components). > > Peter > > ______________________________________________ > 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. >
Bill.Venables at csiro.au
2010-Jun-22 00:22 UTC
[R] how to initial a list to store data result?
The usual method is either lis <- vector("list") or, nearly equivalently, lis <- list() If you know in advance how many components the list will have, there can be a slight advantage in using lis25 <- vector("list", 25) if e.g. you know the list will be of length 25. Bill Venables. -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of song song Sent: Tuesday, 22 June 2010 9:18 AM To: r-help at r-project.org Subject: [R] how to initial a list to store data result? May I ask how to initialize a list? usually I will use " result=list(0) " to do this. is this right? [[alternative HTML version deleted]] ______________________________________________ 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.