0

So I have a list of length 9 where each element is a dataframe. I wanted to extract specific columns from each dataframe in the most efficient way possible, so I used the below function.

Down <- lapply(tables, "[", 2)

This successfully extracted the information I wanted, but why? What is "[" and how does it satisfy the semantics of lapply?

Vyvanse
  • 1
  • 1
  • 4
    See `?\`[\``. - `lapply(tables, "[", 2)` applies the extractor function `[` to the second element of each object in the tables list, so it's the equivalent of `list(tables[[1]][2], tables[[2]][2], ..., tables[[9]][2])`. – Ritchie Sacramento Oct 25 '22 at 02:51
  • 1
    Also see the [r4ds section on subsetting](https://r4ds.had.co.nz/vectors.html#vector-subsetting) for an intro, and the [Advanced R chapter](https://adv-r.hadley.nz/subsetting.html) for a deeper dive. – zephryl Oct 25 '22 at 02:57
  • 2
    Also relevant [Difference between `[` and `[[`](https://stackoverflow.com/questions/1169456/the-difference-between-bracket-and-double-bracket-for-accessing-the-el) – Rui Barradas Oct 25 '22 at 03:31

1 Answers1

1

To augment Ritchie Sacramento's very nice explanation, you can also access the dataframe columns in a list using conventional notation:

cars1 <- mtcars
cars2 <- cars1
cars3 <- cars2
tables <- list(cars1, cars2, cars3)
lapply(tables, function(x) x$cyl)
[[1]]
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

[[2]]
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

[[3]]
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
lapply(tables, function(x) x[, 2])
[[1]]
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

[[2]]
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4

[[3]]
 [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
SteveM
  • 2,226
  • 3
  • 12
  • 16