1

I have a list of lists of 1 data frame each, as in the following example:

> t
[[1]]
  y1 y2
1  1  4
2  2  5
3  3  6

[[2]]
  y1 y2
1  3  6
2  2  5
3  1  4

This of course gives me results like:

> class(t[1])
[1] "list"
> class(t[[1]])
[1] "data.frame"

Maybe I'm using R wrong, but this annoys me. I'd rather just have a list of data frames. That way, I can refer to each one as t[i], instead of the clunky t[[i]]. Whats the best way to turn this into a list of data frames?

Am I just way off base here? R seems to love giving me these singleton lists.

Thanks in advance for your help, and patience.

Plastic Soul
  • 484
  • 7
  • 19
  • Try using `t[[i]]` instead of `t[i]`. (And carefully read the section on `"Recursive (list-like) objects"` in `help("[")` for the documentation of the distinction in this setting between `[` and `[[`.) – Josh O'Brien Nov 12 '14 at 19:18
  • Thanks! I think I understand the distinction -- I just would prefer to convert the object to one where I can just use `[`. If all the lists are singletons, do I gain any advantage by leaving it in its current form? That's what confuses me. – Plastic Soul Nov 12 '14 at 19:23
  • My recommendation is that you just learn to use `[[` when you want to extract the contents of one list element (rather than a length-one list comprised of that element). FTR, the advantage to having `[` behave as it does is that it allows you to take arbitrary subsets of lists (`t[1:4]` or `t[1]` or `t[-5]` or `t[c(1,1,1,2,2)]`). – Josh O'Brien Nov 12 '14 at 19:36
  • 5
    *I'd rather just have a list of data frames.*, but you do have a list of data frames... Take a look [here](http://stackoverflow.com/questions/2050790/how-to-correctly-use-lists-in-r) for some interesting information regarding lists – David Arenburg Nov 12 '14 at 19:40
  • 1
    Voting to close. This is essentially a request for R to be a different language than it is. – IRTFM Nov 12 '14 at 19:58
  • Thanks for the explanations. I would never request such a thing :) – Plastic Soul Nov 12 '14 at 20:14
  • @DavidArenburg - Thanks for the additional info. I'm trying to understand what you mean. I have a list of lists, rather than a list of data frames. One problem I keep running into is: I have some code that I've applied to one of the data frames in a list called `stream`, for example: `fbvisits<-grep("Facebook",stream[[18]]$url)`, is there a way to apply this to every data frame in `stream`, so that fbvists[i] is a list containing the result of `grep("Facebook",stream[[i]]$url)` (without using a for loop)? Thanks so much for your help – Plastic Soul Nov 13 '14 at 17:32

1 Answers1

0

You already have a list of data frames and cannot do anything further (except row binding them to produce a single data frame). Try reading the subsetting chapter in the Advanced R book.

In brief

R has 3 primary subsetting methods, [, [[, and $. Their behavior depends on what object you use them on. (@ is rarer and used for S4 objects.)

Generally speaking, [ subsetting returns a subset of the same object you had before. For instance, (1:5)[4] returns a length 1 vector from a length 5 vector. [[ subsetting returns part of the object not necessarily but maybe of the same type as what you had. For vectors, it does the same as [.

Since lists often contain objects of different types, the difference between [ and [[ can matter a lot. [ subsetting gives you a new list with only the specified elements, while [[ gives you the elements directly. Note that data frames are lists. iris[1] returns a data frame with only the first column. iris[[1]] returns a vector, the first object from the iris data frame.

$ is merely a synonym for [[ that allows autocompletion.

The distinction between [[ and [ can get non-obvious for some objects. For instance, iris[[c(1, 2)]] does the same as iris[2, 1]. The first seems to select object 1, then object 2. The second selects row 2 in column 1.

CoderGuy123
  • 6,219
  • 5
  • 59
  • 89