From gavin.simpson at ucl.ac.uk  Mon Sep 19 18:05:07 2005
From: gavin.simpson at ucl.ac.uk (Gavin Simpson)
Date: Mon, 19 Sep 2005 17:05:07 +0100
Subject: [Rd] Lists and data frames (PR#8143)
In-Reply-To: <432ECDBD.4060303@statistik.uni-dortmund.de>
References: <20050919133405.D29FD1C6B0@slim.kubism.ku.dk>
	<1127138624.31636.6.camel@gsimpson.geog.ucl.ac.uk>
	<432ECDBD.4060303@statistik.uni-dortmund.de>
Message-ID: <1127145907.8144.14.camel@gsimpson.geog.ucl.ac.uk>

On Mon, 2005-09-19 at 16:39 +0200, Uwe Ligges wrote: 
> Gavin Simpson wrote:
> 
> > On Mon, 2005-09-19 at 15:34 +0200, fwagner at fh-lausitz.de wrote:
> > 
> >>Full_Name: Frank Wagner
> >>Version: R 2.1.1
> >>OS: Windows
> >>Submission from: (NULL) (193.174.73.34)
> >>
> >>
> >>Hi,
> >>The pdf file R-intro descripe on page 27 that lists can be extended by adding
> >>numbers.
> >>Unfortunately, it's not working 
> >>## example :
> >>
> >># if i did not declare the variable an error occurs : object not found
> >>mylist <- list() 
> >>mylist[1] <- list(value1=3, value2=5)
> >>## Error
> > 
> > 
> > You need to use [[x]] to subset a list:
> > 
> > 
> >>mylist <- list()
> >>mylist[[1]] <- list(value1=3, value2=5)
> >>mylist
> > 
> > [[1]]
> > [[1]]$value1
> > [1] 3
> > 
> > [[1]]$value2
> > [1] 5
> 
> 
> This is a list of a list, but that is not the same as the stuff we are 
> discussing here. See below.
> 
> 
> > 
> >>str(mylist)
> > 
> > List of 1
> >  $ :List of 2
> >   ..$ value1: num 3
> >   ..$ value2: num 5
> > 
> > I don't know whether there is a typo on page 27 or not: [x] is valid, it
> > just means something different to [[x]] - as explained on page 26 of
> > said manual. If it was intentional, then IMHO it is not the most clear
> > example of extending a list - the [[x]] notation is what I would expect
> > to have to use - after reading page 26 of course...
> 
> Folks, please specify which version of the manual you are speaking 
> about, e.g. by giving a chapter's/section's name.

R-patched Section 6.1 and 6.2 - (pdf version). Which was stated in
Frank's original email which I included, as was R version info.

> 
> The statement on what is referred to page 27 in this thread is completly 
> correct.

I would say what we are discussing here is a matter of interpreting what
the OP was intending to do. If the OP wanted to replace the first
component of mylist then [[1]] is needed. If it was the first sublist of
mylist then [1] is called for.

I interpreted the OP as the former; wanting to put a list in the first
component of mylist - because that is what the example on page 27 states
it is doing (depending on what "component" means - see below).

Confusion arises, because it depends on what you take "components" to
mean in para 2, page 27. In the paragraph above para 2 on page 27, a
list is defined and "components" refers to the bits extracted by [[ ]].
[x] extracts a list containing the xth component. So when para 2 states
that if you wish to add more components to the list you use [ ], isn't
this contradicting the previous paragraph?

mylist <- list(comp1 = 1, comp2 = matrix(1:10, ncol = 2), comp3 =
"comp3")
> mylist
$comp1
[1] 1

$comp2
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

$comp3
[1] "comp3"

> mylist[[2]]
     [,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10

> mylist[1] <- list(comp5 = 1:10, comp6 = 1:10)
Warning message:
number of items to replace is not a multiple of replacement length
> mylist[[1]] <- list(comp5 = 1:10, comp6 = 1:10) ## or
> mylist[1] <- list(list(comp5 = 1:10, comp6 = 1:10))

And here I *do* want to replace the first component with a list (itself
with two components)

Which is because [[ ]] extracts the "component", whereas [ ] extracts a
[sub]list:

> class(mylist[[2]])
[1] "matrix"
> class(mylist[2])
[1] "list"

So, it depends what you mean by a "component".

At the very least, the use of "component" in the first two paragraphs of
page 27 (pdf version) is confusing as the two uses do not correspond to
the same "thing". I would go as far as saying contradictory - but that
might be nit-picking and depends on your definition of "extracts" ;-)

Wouldn't the following be better:

Lists, like any subscripted object, can be extended by specifying
additional components. To add new /components/ you could:

> Mat <- matrix(1:100, ncol = 10) ## not defined previously
> ## add Mat to component 5 of Lst
> Lst[[5]] <- Mat
> ## or
> Lst[5] <- list(Matrix = Mat)
> ## or, replace Lst[[5]] with a list with 2 components
> Lst[[5]] <- list(Matrix1 = Mat, Matrix2 = Mat)

See Section 6.1 for the differences.

Personally, I find Lst[[5]] <- Mat more intuitive than having to wrap it
in list().

Just my USD1.50 worth (judging by the length of this email)

G

> 
> Note that a list is nothing else than a vector of mode list which 
> contains in each element a list of length one.
> 
> Hence you *can* say
> mylist[1:2] <- list(value1=3, value2=5)
> or
> c(mylist, list(value1=3, value2=5))
> or whatever.
> 
> 
> Uwe Ligges
> 
> 
> 
> > HTH
> > 
> > G
> 
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [T] +44 (0)20 7679 5522
ENSIS Research Fellow             [F] +44 (0)20 7679 7565
ENSIS Ltd. & ECRC                 [E] gavin.simpsonATNOSPAMucl.ac.uk
UCL Department of Geography       [W] http://www.ucl.ac.uk/~ucfagls/cv/
26 Bedford Way                    [W] http://www.ucl.ac.uk/~ucfagls/
London.  WC1H 0AP.
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



[Rd] Lists and data frames (PR#8143)
Gavin Simpson