A tutorial for writing functions in R (CC177)

preview_player
Показать описание
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

#attr #Rstats #baseR #str

You can also find complete tutorials for learning R with the tidyverse using...

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
Рекомендации по теме
Комментарии
Автор

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 ;)

dariushghasemi
Автор

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 :)

taylorwright
Автор

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.

yeokcmark
Автор

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!

jonf
Автор

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

ac
Автор

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! 💥💥💥

dmalarekable
Автор

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

bassamabdelnabi
Автор

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

marc_CFA_applied_econ
Автор

Thanks a lot for this video. Trying it out today

elijahrona
Автор

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.

drjzee
Автор

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

Moon-oxil
Автор

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.

PA_hunter
Автор

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

Psylinguist
Автор

sir can i request for videos you to create?

krushnachChandra
Автор

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

samuelowusuansah
Автор

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

HanySalem
Автор

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 <- function(df, df1, v1, values)
{
df1 <- merge(x=subgroup, y=df, by="USUBJID", all.x = TRUE) %>%
mutate(test= case_when(is.na(v1) ~ "Not Reported",
TRUE ~ v1)) %>%
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.

abolit