Introduction to Repeating Things in R: Looping Over Files

Поделиться
HTML-код
  • Опубликовано: 23 ноя 2024

Комментарии • 20

  • @mocabeentrill
    @mocabeentrill Год назад +1

    😂😂😂. The last bit cracks me up! "I'm retiring"

  • @thethingsilove902
    @thethingsilove902 3 года назад +1

    This video was amazing! Thank you so much for taking the time to explain everything in detail. I followed along and it worked smoothly.

    • @weecology
      @weecology  3 года назад

      Thanks! That’s awesome!

  • @vanessamohr8073
    @vanessamohr8073 2 года назад

    Thanks for explaining this! The video is great and it helped me a lot :)

  • @MJ-hc9qk
    @MJ-hc9qk 2 года назад

    Great video, thanks for the help. What if I wanted to actually load in all the data files to the environment, how would I make the "data" in line 9 be a variable?
    thanks in advance

    • @weecology
      @weecology  2 года назад +1

      You wouldn't make the variable name `data` itself be a variable. There are typically two approaches to what you want to accomplish:
      1) make `data` a list and store each data frame loaded by the loop as one position in that list, so the first time through the loop the data frame gets stored in the first position in the list, the second time through the loop it gets stored in the second position, and so on. You can see some of the basic ideas in our Looping By Index video: ruclips.net/video/vWj5rypEZ4U/видео.html The modified version of the code would look like:
      data

  • @larissacury7714
    @larissacury7714 2 года назад

    Thank you very much, but what if instead of getting a single vector called "results" I want to create a different data frame for each of the data files?

    • @weecology
      @weecology  2 года назад +1

      You're welcome! There's an answer to another question that lays out the two basic strategies when working with multiple data frames. Take a look and let me know if you have any more questions: ruclips.net/video/4-uWgh5kDSc/видео.html&lc=UgwqqFrWYTHi7aVTh7B4AaABAg.9bGLQ65trTb9bHwBtID_eI

    • @larissacury7714
      @larissacury7714 2 года назад

      @@weecology Thank you very much, but I guess that this link is the same of this video, isn't it?

    • @weecology
      @weecology  2 года назад +1

      @@larissacury7714 It should link to the comment, but it looks like it's doing something funny when clicked on from the comments section. It's the response to the comment M J 2 months ago. Here's a copy of it:
      There are typically two approaches to what you want to accomplish:
      1) make `data` a list and store each data frame loaded by the loop as one position in that list, so the first time through the loop the data frame gets stored in the first position in the list, the second time through the loop it gets stored in the second position, and so on. You can see some of the basic ideas in our Looping By Index video: ruclips.net/video/vWj5rypEZ4U/видео.html The modified version of the code would look like:
      data

    • @larissacury7714
      @larissacury7714 2 года назад

      @@weecology thank you!! I'll take a look at it now!

  • @DavidAckuaku
    @DavidAckuaku 10 месяцев назад

    How can I use for loop to read in different file with with subscript in the file name. Example if I am reading in the first file, I want data1 and for second file, I want data2 and so on. Thanks for any help.

    • @weecology
      @weecology  10 месяцев назад

      You could do something like this:
      for (i in 1:num_files){
      filename

    • @DavidAckuaku
      @DavidAckuaku 10 месяцев назад

      @@weecology Thanks.

  • @АлексейДемин-ъ5ж

    oh men. its so short

  • @mhd.wathekalhajkhalaf7969
    @mhd.wathekalhajkhalaf7969 2 года назад

    i have a data.frame with 17 column I want to replace each value by this formula
    new value=0.5+(0.5(x(each value in column)-mean(column)/(max(column)-min(column))

    • @weecology
      @weecology  2 года назад +1

      There are a few different ways to do this. To do it with a for loop you loop over the columns of the data frame using and index and then perform your calculation on each column storing it back into the original column:
      for (i in seq_along(df)) {
      df[i] = 0.5 + (0.5 * (df[[i]] - mean(df[[i]])/(max(df[[i]]) - min(df[[i]]))))
      }
      Our video on looping by index (ruclips.net/video/vWj5rypEZ4U/видео.html) will help explain some of the work with i. The one trick piece is the use of two sets of square brackets, which converts the column of the data frame into a vector so that mean, max, and min can work on it.
      The simplest way to do this is probably to use mutate_all() from dplyr:
      mutate_all(df, funs("normalized" = 0.5 + (0.5 * (.-mean(.)/(max(.)-min(.))))))
      This will create a new column for each of your 17 columns with the word _normalized applied to the end applying the function to the data in the original column.

  • @kamilasultanova3068
    @kamilasultanova3068 2 года назад +1

    too much water