Because I remember things better when I explain them, dentist and because accessing elements inside a list in R is just barely different enough from other programming languages I’m used to that it’s throwing me off with false cognates a bit — here’s a brief tour on basic ways to access objects in an R list. This is elementary R, but I’m a novice R programmer taking notes for my own reference, so hey.

Note that the “>” at the start of some lines is the R prompt (“stuff you should type”), but that you shouldn’t type the “>.” Everything else is print output that R will display to you.

First we need to create a list which we’ll call mylist.

> mylist <- list(component = c(1,2,3))

mylist is a super-simple list. Its contents look like this:

> mylist
$component
[1] 1 2 3

This list only has one element. It is named component. (It also has one attribute, as Jerzy pointed out in the comments below — the single attribute is called “names”.)

> attributes(mylist)
$names
[1] "component"

We can access the element named component in a few ways:

> mylist["component"]
> mylist[1]

One uses the element name, the other uses the index number. Both of these return a list, which looks like this when it’s printed out — note the $component at the top. Inside the list is a vector of 3 numbers.

$component
# [1] 1 2 3

If you want to reach inside the list and get the stuff inside — in this case, that vector of 3 numbers — you can do it two ways as well: with the attribute name or the index number.

> mylist$component
> mylist[[1]]

Both of them will return simply the vector — it won’t be wrapped in a list. Note how there’s no $component label at the top.

[1] 1 2 3

Since mylist$component and mylist[[1]] return whatever is inside this list, and in this case it is a vector, we can access elements in it just like any other vector.

Side note: R typically gives you columns (for instance, mymatrix[1] gives you the first column of mymatrix), but if you throw a comma afterwards it’ll give you rows. (For instance, mymatrix[1,] gives you the first row of mymatrix). You can combine these: mymatrix[c(1,2,3),c(1,2)] gives you the first 3 rows of the first 2 columns of mymatrix.

Know someone who'd appreciate this post?

3 Responses to “R basics: accessing elements of an object”

Nice, Mel! Just a few nitpicks: mylist there does have one attribute, but that is “names” not “component”. I think “component” would be referred to as an *element* of the list.

You can also access it via
mylist[["component"]]
but I’m not fully clear on how this differs from
mylist["component"]
or
mylist$component

Finally, your code shows $gt; rather than the less-than sign; is there a way to fix that? And the last sentence should say “first 3 rows of the first 2 columns”.

The RHIPE group hasn’t met for about 2 weeks because of fall break and… other things I don’t know. I’m eager to get a move on things this week, because it’s nearly halfway through the term and I want to DO COOL STUFF WITH HADOOP, DARN IT.

I might just start sitting in the back and working tutorials on my own again, though writing my readiness (comps/quals/prelim equivalents for my department) has my brain otherwise occupied ’till the end of November.

Do you know any way of getting a handle on the INDICES of a list? I want to use these as vertex labels in an igraph cluster subnetwork rather than a list of the members. As above, ‘mulist$component[2] returns the contents of the component; what I want is just a vector of numbers corresponding to 1,2,3,etc.– and I HAVE tried to get away with declaring that concatenation explicitly but it didn’t work. I have been looking everywhere for this info– would be nice if you let me know if in fact you do know. Thanks anyway of course -