A tutorial for writing functions in R (CC177)

แชร์
ฝัง
  • เผยแพร่เมื่อ 30 มิ.ย. 2024
  • Writing your own R functions is critical to expanding the versatility and reproducibility of your code. In this epsiode Pat shares almost everything you'd ever want to know about writing your own R functions and then applies this knowledge to adapt the code he's been writing in this series into a 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).
    #attr #Rstats #baseR #str
    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 Creating your own functions in R
    1:52 Syntax for writing a simple function
    3:28 How to use your custom functions
    6:12 Assigning default values to arguments
    6:50 Creating more complex functions
    8:17 Determining what gets returned from a function
    9:56 Converting our code into a function
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    I've been waiting a long time for tutorials on writing more complex functions in R, hope to see more towards useful and reproducible functional programming in R. Thanks a million dear Pat ;)

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

      Sorry. I really don’t know what you’re looking for. I can’t make up a more complex function out of thin air 😂 complex functions are relatively simple ones with more stuff going on

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

      @@Riffomonas I would also like to see some functions where one can use variable names in arguments to create box or hist plots on all numeric variables or any such examples where {}, quo, enquo comes into the play.

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

      or converting datatypes of date type looking columns by using lubridate & just giving the column names to the function.
      I am sure there are many such scenarios when we are working with the real life data and we do not want to repeat ourself but use same code on multiple variable names. For example - custom univariate analysis by data types may be.

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

    I most likely "late" in commenting on the video since this was uploaded in January of this year; but I have been struggling with creating functions. This is very helpful as it provides step-by-step, the elements of the function and how to create it as well as a giving a very good example at the end on creating more "complex" function!

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

      Wonderful- glad it was helpful! 🤓

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

    Hey, Pat!
    I always love the R content but I do also want to say that I'm really impressed with the strides your video editing has made since you began!
    Great work :)

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

    I've been struggling with microbiome analysis in R for my PhD for a few months now. Luckily, I've found your tutorials on R a few weeks ago and since then, my understanding of this language grew a lot! Pat, you are awesome! 💥💥💥

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

      Awwww, you’re too kind! Thanks for tuning in

  • @yeokcmark
    @yeokcmark 3 หลายเดือนก่อน

    Thank you for all your videos. As a student starting out in R, I’ve learnt so much from you.
    Great to see that you’ve resumed making videos with the latest one on writing your own packages.

    • @Riffomonas
      @Riffomonas  3 หลายเดือนก่อน

      Wonderful! Thanks for watching

  • @ac6852
    @ac6852 ปีที่แล้ว +3

    Incredible tutorial!!! So clear, concise and helpful! Thank you so much for creating this! Subscribed. ✨

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

    Thanks Pat. I always pick up something new from your videos, like setting default values in this case - who knew?

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

      Awesome 🤩 I wanted to show how to do defaults but couldn’t think of a natural way to do it. Glad you enjoyed it!

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

    Simple and easy…. You are gifted with what you do… thanks so much for sharing

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

      My pleasure! Thanks for watching 🤓

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

    awesome function tutorial💌

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

    Best explanation!

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

    Thanks a lot for this video. Trying it out today

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

    Thank you for this great video!

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

    Thanks Pat! A very useful introduction to functions.
    LOL 'only place in the world that uses Farenheit'. Along with MM-DD-YYYY which is a personal point of pain :). As with much of these things - it bleeds over into Canada quite often.

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

      We’ll no one uses the correct format of YYYY-MM-DD 😂

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

      @@Riffomonas Ah! Actually I do. I shifted over to this in the last year (require conscious effort) as we use it at work and I saw great twitter advise on the benefits of using it for naming files (such as manuscript versions). Works great with paste0 outputs using Sys.Date(). :)

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

    Thank you

  • @Moon-ox4il
    @Moon-ox4il 2 ปีที่แล้ว +1

    God I am such a scrub at making functions. this vid def helped! thanks :)

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

      Wonderful! I’m glad this episode was helpful

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

    Do you do ML? Would love to see vids on how to present ML findings in academia. I find that ML is difficult to explain to non ML users, so good practices with visualizations, etc would be helpful for communication. Regression is nice because it has coefficients, but beyond regression it gets more difficult to explain, I think.

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

      Here’s a series using a package we created Machine Learning
      th-cam.com/play/PLmNrK_nkqBpKpzb9-vI4V7SdXC-jXEcmg.html

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

    sir can i request for videos you to create?

  • @Psylinguist
    @Psylinguist 5 หลายเดือนก่อน

    f in the video stands both for fahrenheit and freezing.What is freezing? how do you measure it?

    • @Riffomonas
      @Riffomonas  3 หลายเดือนก่อน

      In Fahrenheit, freezing is -32F/0C

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

    I want an easy way of manipulating netcdf files and displaying it spatially with maps on r

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

      Thanks for watching Samuel! I hope this helped you learn more about working with netcdf files

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

    I have an experience when the code works but the function , as I wrapped the code into a function, failed ... did nothing. any suggestions.

    • @Riffomonas
      @Riffomonas  หลายเดือนก่อน +1

      Are you maybe missing a return statement ?

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

      @@Riffomonas I think so. Thank you for reminding me

  • @abolit9831
    @abolit9831 5 หลายเดือนก่อน

    Can we use case_when argument for missing value when creating a function in R ?
    I am attempting to write a function but getting error for case_when argument and a variable passed in that argument. Can you help with this ?
    Code written :
    setNR %
    distinct(USUBJID, TRT01P.x, TRT01PN.x, test, .keep_all = TRUE )
    }
    setNR(df=adcm1, df1=adslcm, v1=FUACTRT, values=NA)
    Error observed :
    Error in `mutate()`:
    ! Problem while computing `test = case_when(is.na(v1) ~ "Not Reported", TRUE ~ v1)`.
    Caused by error:
    ! object 'FUACTRT' not found
    Run `rlang::last_error()` to see where the error occurred.

    • @Riffomonas
      @Riffomonas  3 หลายเดือนก่อน

      The problem appears to be that your data frame doesn't have a FUACTRT column