Double square brackets in R - Dave Tang's blog (2024)

Table of Contents
Conclusions See more

This deserved its own post because I had some difficulty understanding the double square brackets in R. If we search for "double square brackets in R" we come across this tutorial, which shows us that the double square brackets, i.e. [[]], can be used to directly access columns:

head(iris) Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa4 4.6 3.1 1.5 0.2 setosa5 5.0 3.6 1.4 0.2 setosa6 5.4 3.9 1.7 0.4 setosa#vector of sepal lengths using the column nameiris[['Sepal.Length']] [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7 5.1 5.4 5.1 [23] 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 [45] 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 [67] 5.6 5.8 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 [89] 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2[111] 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9[133] 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9#vector of sepal lengths using the column indexiris[[1]] [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7 5.1 5.4 5.1 [23] 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 [45] 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 [67] 5.6 5.8 6.2 5.6 5.9 6.1 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 [89] 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2[111] 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0 6.9 5.6 7.7 6.3 6.7 7.2 6.2 6.1 6.4 7.2 7.4 7.9[133] 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9#the double square brackets in R can also be used#with the single square bracketsiris[[1]][2][1] 4.9

More technically, the R manual states that one generally uses [[ to select any single element for lists and the [[ form allows only a single element to be selected using integer or character indices.

Take for example this list created following this tutorial:

#for upper, the first twenty letters are removed#and thus we're left with the last sixmy_list <- list(lower=letters[1:4], upper=letters[-1:-20])my_list$lower[1] "a" "b" "c" "d"$upper[1] "u" "v" "w" "x" "y" "z"#using [[]] we can access the lower characters of the alphabetmy_list[['lower']][1] "a" "b" "c" "d"my_list[['upper']][1] "u" "v" "w" "x" "y" "z"

I can see how the [[]]'s are quite useful when we need to reference elements within a list that have a list.

I would also like to point out that [[ can be used as a function. This is handy when you have a list within a list.

my_list <- list(one = list(first = "Bob", last = "Smith"), two = list(first = "Jane", last = "Turner"))# retrieve entry "one"`[[`(my_list, "one")$first[1] "Bob"$last[1] "Smith"# retrieve "last" from all entrieslapply(my_list, `[[`, "last")$one[1] "Smith"$two[1] "Turner"

Sometimes the results returned from an analysis package is a list, which I want to convert to a data frame (say for merging reasons). Below is some code to obtain gene symbols for probe ids (as per my post on Using the Bioconductor annotation packages) and how to convert this list into a data frame:

#install if necessarysource("http://bioconductor.org/biocl*te.R")biocl*te("illuminaMousev1p1.db")library("illuminaMousev1p1.db") gs <- illuminaMousev1p1SYMBOLgs_probe <- mappedkeys(gs) head(gs_probe)[1] "ILMN_1212602" "ILMN_1212605" "ILMN_1212607" "ILMN_1212610" "ILMN_1212612" "ILMN_1212614" gs_probe_lookup <- as.list(gs[gs_probe])head(gs_probe_lookup)$ILMN_1212602[1] "Best1"$ILMN_1212605[1] "1500011K16Rik"$ILMN_1212607[1] "Cradd"$ILMN_1212610[1] "Zfp626"$ILMN_1212612[1] "Rcan2"$ILMN_1212614[1] "Med12l"#refer to the names of the listhead(names(gs_probe_lookup))[1] "ILMN_1212602" "ILMN_1212605" "ILMN_1212607" "ILMN_1212610" "ILMN_1212612" "ILMN_1212614"#refer to only the symbols#use unlist but this keeps the nameshead(unlist(gs_probe_lookup)) ILMN_1212602 ILMN_1212605 ILMN_1212607 ILMN_1212610 ILMN_1212612 ILMN_1212614 "Best1" "1500011K16Rik" "Cradd" "Zfp626" "Rcan2" "Med12l"#so use the parameter use.names=F to remove themhead(unlist(gs_probe_lookup, use.names=F))[1] "Best1" "1500011K16Rik" "Cradd" "Zfp626" "Rcan2" "Med12l"#voilaprobe_to_symbol <- data.frame(probe=names(gs_probe_lookup), symbol=unlist(gs_probe_lookup, use.names=F))head(probe_to_symbol) probe symbol1 ILMN_1212602 Best12 ILMN_1212605 1500011K16Rik3 ILMN_1212607 Cradd4 ILMN_1212610 Zfp6265 ILMN_1212612 Rcan26 ILMN_1212614 Med12l

Conclusions

The double square brackets in R can be used to reference data frame columns, as shown with the iris dataset. An additional set of square brackets can be used in conjunction with the [[]] to reference a specific element in that vector of elements.

I can see that the [[]]'s are much more useful in lists that are structured as my_list in the example above. To flatten the list structure use the unlist() function, which can be useful when you want to convert a list into a data frame.

So how did a post on double square brackets in R turn into a post about lists? Initially, I thought I could use the double square brackets to refer to the elements in a list given a list structure (such as the results from using the annotation package). However I was unable to and the best solution seems to be flattening the list using unlist() and the use.names=F parameter.

See more

The R Language Definition manual.

Greetings, enthusiasts of R programming! Allow me to share my expertise on the intricate use of double square brackets in R, a topic that often perplexes users. My extensive experience in R programming and data manipulation positions me well to unravel the nuances of this syntax.

In the provided article, the author delves into the application of double square brackets ([[]]) in R, primarily for accessing columns in a data frame. Let's break down the concepts covered in the article:

  1. Accessing Columns in a Data Frame: The double square brackets can be employed to directly access columns in a data frame. For instance, using the iris dataset:

    # Vector of sepal lengths using the column name
    iris[['Sepal.Length']]
    
    # Vector of sepal lengths using the column index
    iris[[1]]

    This syntax facilitates extracting specific columns efficiently.

  2. Nested Lists and Double Square Brackets: The article demonstrates the utility of double square brackets when dealing with nested lists. For example:

    # Accessing elements in a nested list
    my_list <- list(lower=letters[1:4], upper=letters[-1:-20])
    my_list[['lower']][1]
    my_list[['upper']][1]

    Double square brackets are particularly handy when navigating through lists within lists.

  3. [[ as a Function: An intriguing aspect is the use of [[ as a function, allowing extraction of elements from a list within a list:

    # Using [[ as a function to retrieve entries
    `[[`(my_list, "one")$first[1]
    `[[`(my_list, "one")$last[1]

    This can be advantageous when dealing with complex list structures.

  4. Converting Lists to Data Frames: The article explores scenarios where lists obtained from analysis packages need to be converted to data frames. The unlist() function, along with parameters like use.names=F, proves useful in flattening lists.

    # Converting a list to a data frame
    probe_to_symbol <- data.frame(probe=names(gs_probe_lookup), symbol=unlist(gs_probe_lookup, use.names=F))

    This process is demonstrated using gene symbols for probe IDs.

  5. Conclusion: The article concludes by emphasizing the versatility of double square brackets, not only for referencing data frame columns but also for navigating through complex list structures. It highlights the importance of unlist() when converting lists to data frames.

In essence, the article provides a comprehensive understanding of the applications of double square brackets in R, showcasing its versatility in handling data frames and lists. For further reference, the author recommends consulting "The R Language Definition" manual.

Double square brackets in R - Dave Tang's blog (2024)
Top Articles
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6111

Rating: 4.8 / 5 (48 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.