Indexing and looping over lists in R, a tutorial (CC174)

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 มิ.ย. 2024
  • Lists are an important, but underappreciated type of data in R. In this Code Club, Pat gives a tutorial on indexing or accessing values from a list and then shows how we can iterate over the values of a list using the lapply function. Along the way we'll see how we can unlist lists and convert them into matrices as well as generate the transpose of a matrix with the t function. This is part of a series of videos demonstrating how to functions from base R to make these data compatible with tools from the tidyverse
    You can find my blog post for this episode at www.riffomonas.org/code_club/....
    The data were generated in our Kozich et al. 2013 paper (doi.org/10.1128/AEM.01043-13) using samples from the Schloss et al. 2012 paper (doi.org/10.4161/gmic.21008).
    #list #lapply #R #Rstats #baseR
    Want more practice on the concepts covered in Code Club? You can sign up for my weekly newsletter at shop.riffomonas.org/youtube to get practice problems, tips, and insights.
    If you're interested in taking an upcoming 3 day R workshop be sure to check out our schedule at riffomonas.org/workshops/
    You can also find complete tutorials for learning R with the tidyverse using...
    Microbial ecology data: www.riffomonas.org/minimalR/
    General data: www.riffomonas.org/generalR/
    0:00 Using lists and lapply to replace a for loop
    1:31 Parsing a file on line breaks with scan
    3:53 Splitting text data with strsplit
    5:57 How to access values from a list
    8:35 Creating a function to make list elements the same length
    11:43 Iterating a function over values in a list with lapply
    13:51 Merging elements of a list with unlist and do.call
    16:14 Converting lower triangular matrix to a full matrix
  • วิทยาศาสตร์และเทคโนโลยี

ความคิดเห็น • 19

  • @CBG91
    @CBG91 2 ปีที่แล้ว +2

    Your explanation that dataframes are actually lists of vectors blew my mind. None of my instructors have ever made that explicit!

    • @Riffomonas
      @Riffomonas  2 ปีที่แล้ว +1

      Wonderful! Thanks for watching 😊

  • @dimamedvedev4124
    @dimamedvedev4124 7 หลายเดือนก่อน

    great exapmles and smart explanation

  •  2 ปีที่แล้ว +1

    Learning a lot from your videos. Happy holidays!

    • @Riffomonas
      @Riffomonas  2 ปีที่แล้ว

      Hi Victor - wonderful! Thanks for watching

  • @luisandresmorenocueva3399
    @luisandresmorenocueva3399 2 ปีที่แล้ว +1

    Merry xmas. Thx for the videos of this year

    • @Riffomonas
      @Riffomonas  2 ปีที่แล้ว

      Thanks Luis! Have a great Christmas 😊

  • @marcossahade9369
    @marcossahade9369 2 ปีที่แล้ว +1

    very interesting lessons .. congrats from Argentina

    • @Riffomonas
      @Riffomonas  2 ปีที่แล้ว

      Hi Marcos - Thanks for watching!

  • @tlange5091
    @tlange5091 2 ปีที่แล้ว +1

    Thank you so much for your videos!

    • @Riffomonas
      @Riffomonas  2 ปีที่แล้ว

      My pleasure! Thanks for tuning in 🤓

  • @oluwafemioyedele
    @oluwafemioyedele 2 ปีที่แล้ว +1

    Nice tutorial!!!

    • @Riffomonas
      @Riffomonas  2 ปีที่แล้ว

      Thanks- glad it was useful!

  • @roymccormick5328
    @roymccormick5328 ปีที่แล้ว +1

    cool thx

    • @Riffomonas
      @Riffomonas  ปีที่แล้ว

      Thanks for watching!

  • @domenicoscarpino3715
    @domenicoscarpino3715 ปีที่แล้ว

    Hi, thanks a lot for the great content.
    I don't seem to be able to explain why they included [,1] in the code below. I was checking a solved exercise and found this code, so I thought I could maybe ask here about the theoretical explanation behind that specific part. I would have thought that Data[,i] was enough to make it work. Thanks in advance.
    for(i in c(2,6:11)){
    a=(abs(scale(Data[,i])[,1])

    • @Riffomonas
      @Riffomonas  ปีที่แล้ว

      Thanks for the question. Unfortunately, I am unable to answer questions like this in comments. I'd encourage you to ask on StackOverflow or on the RStudio forum