Skip to content

Lists and data frames (PR#8143)

4 messages · fwagner@fh-lausitz.de, Uwe Ligges, Gavin Simpson

#
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

Can you please help me
Thank you

Regards
Frank Wagner
#
On Mon, 2005-09-19 at 15:34 +0200, fwagner at fh-lausitz.de wrote:
You need to use [[x]] to subset a list:
[[1]]
[[1]]$value1
[1] 3

[[1]]$value2
[1] 5
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...

HTH

G
#
Gavin Simpson wrote:

            
This is a list of a list, but that is not the same as the stuff we are 
discussing here. See below.
Folks, please specify which version of the manual you are speaking 
about, e.g. by giving a chapter's/section's name.

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

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
#
On Mon, 2005-09-19 at 16:39 +0200, Uwe Ligges wrote:
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.
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")
$comp1
[1] 1

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

$comp3
[1] "comp3"
[,1] [,2]
[1,]    1    6
[2,]    2    7
[3,]    3    8
[4,]    4    9
[5,]    5   10
Warning message:
number of items to replace is not a multiple of replacement length
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:
[1] "matrix"
[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:
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