March 2021
March 1
This one is an oldie but a goodie:
β R Function A Day (@rfunctionaday) March 1, 2021
The {switch} function from {base} π¦ helps you select from a list of alternative actions depending on a condition of interest πhttps://t.co/IYYI1dIvpm
Note how, if unspecified, values can trickle down.#rstats #DataScience pic.twitter.com/6iqBh85XMY
March 2
In case you want to create a new variable conditional on checking whether values for a different variable lie inside an interval, the {between} function in {dplyr} can be a big help πhttps://t.co/JrEHPo5oDv
β R Function A Day (@rfunctionaday) March 2, 2021
Works with date-time objects as well! π#rstats #DataScience pic.twitter.com/J5TtCwLBE3
March 3
Sometimes you might wish to know which π¦ some of the unfamiliar functions are from, or where some of the objects in your session are located.
β R Function A Day (@rfunctionaday) March 3, 2021
The {find} function in {base} π¦ can be helpful here πhttps://t.co/vTvhBnFogF#rstats #DataScience pic.twitter.com/BG9E8cnS9w
March 4
No matter the programming language I code in, I rarely wish to leave the comfort zone of #rmarkdown π‘
β R Function A Day (@rfunctionaday) March 4, 2021
But how does one check details about the languages currently supported? Using the {knit_engine} function from {knitr} π§Ά https://t.co/bVEzn990dE#rstats #DataScience pic.twitter.com/LCHtsb1b1T
March 5
If you are used to creating tibble columns sequentially, you might think a similar procedure works while creating lists, but it doesnβt, at least not with {base::list}.
β R Function A Day (@rfunctionaday) March 5, 2021
You can instead use {lst} function from {tibble} π¦! π https://t.co/K97EBCEsYz#rstats #DataScience pic.twitter.com/p152AVXFDb
March 6
Most R packages are standing on the shoulders of other packages.
β R Function A Day (@rfunctionaday) March 6, 2021
In case you are curious to find out which ones, you can use the {plot_dependencies} function from {deepdep} π¦ for visualizing the said dependence:https://t.co/3tl1A7aMW3#rstats #DataScience pic.twitter.com/dUeHWz3MBA
March 7
Missing values can be troublesome for some statistical analysis and need to be attended to during the data exploration phase.
β R Function A Day (@rfunctionaday) March 7, 2021
The {vis_dat} function from {visdat} π¦ makes it effortless to glean the structure of missing values πhttps://t.co/ciff6eE06w#rstats #DataScience pic.twitter.com/eM4uJgWApO
March 8
In case you want to access statistics about your system memory without leaving R, you can use the {ps_system_memory} function from the {ps} π¦https://t.co/hSVUebKkIG
β R Function A Day (@rfunctionaday) March 8, 2021
The measure to watch out for is the available memory.#rstats #DataScience pic.twitter.com/D4YoWLNsku
March 9
Sometimes your data is tabled, i.e.Β a single row corresponds to multiple observations, not one.
β R Function A Day (@rfunctionaday) March 9, 2021
To convert it to tidy data, you can untable it using the {uncount} function from {tidyr} π¦https://t.co/LVXog4CWVM#rstats #DataScience pic.twitter.com/r7tcMVkIFL
March 10
Sometimes you have multiple functions that you always use in conjunction and wish you can compose them into a single function.
β R Function A Day (@rfunctionaday) March 10, 2021
Thanks to {compose} function from {purrr} π¦, you can do exactly that! πΌhttps://t.co/zdydXfEM35#rstats #DataScience pic.twitter.com/teZ5w65uHS
March 11
If you like a certain color from a palette and wish to use it in your plots, you need to find out whatβs the hexadecimal code for that color.
β R Function A Day (@rfunctionaday) March 11, 2021
You can use the {show_col} function from {scales} π¦ to achieve this π¨https://t.co/vxlr8ff035#rstats #DataScience pic.twitter.com/UDN1IFMvO9
March 12
If you want to substitute a certain string pattern in data, you can use {gsub}. But what if you wish to do this across files?
β R Function A Day (@rfunctionaday) March 12, 2021
You can use the {gsub_file} function from {xfun} π¦ to do such substitutions! πhttps://t.co/1PFI7vdRn0#rstats #DataScience pic.twitter.com/09C9GNbKPi
March 13
If you have tidy data and wish to carry out operations that require the data to be in wide format, the βspread-operate-retidyβ pattern can be a bit cumbersome.
β R Function A Day (@rfunctionaday) March 13, 2021
The {pairwise_} function family from {widyr} π¦ makes this less painful:https://t.co/sIcFOKSJSM#rstats #DataScience pic.twitter.com/iaQTtAmzPS
March 14
While working with text data, sometimes you can have a long running paragraph that needs to be wrapped so that it is more readable (and pretty). π
β R Function A Day (@rfunctionaday) March 14, 2021
The {str_wrap} function from {stringr} does the trick! π‘https://t.co/OF9drlXHl0#rstats #DataScience pic.twitter.com/8QVlPZ0vTW
March 15
If you have regression model with interaction effect, it can be tricky to visualize the interaction in a statistically informative way.
β R Function A Day (@rfunctionaday) March 15, 2021
Thankfully, {interplot} π¦ has already done that thinking for you in its eponymous function πhttps://t.co/DhhrVrUxyG#rstats #DataScience pic.twitter.com/rWYxVqnpVV
March 16
In case you are used to the python-style tuple assignment, you can also do something similar in R using the infix operator from {tidytidbits} π¦!https://t.co/0g6LDoZfmq#rstats #DataScience pic.twitter.com/M14Qt9p1rg
β R Function A Day (@rfunctionaday) March 16, 2021
March 17
To report posterior distribution of a Bayesian model, you might report a point estimate, uncertainty around it, and an index for importance of the effect.
β R Function A Day (@rfunctionaday) March 17, 2021
You can do all this using {describe_posterior} function from {bayestestR} π¦https://t.co/OsEGXjo0Nr#rstats #DataScience pic.twitter.com/68fHWiuOM1
March 18
If you see a beautiful image and think to yourself: "I would really like to use that color palette in my plot!", you can do so! π¨
β R Function A Day (@rfunctionaday) March 18, 2021
You can extract the color palette from image using the {create_palette} function from {paletter} π¦https://t.co/uZsB1ovCsf#rstats #DataScience pic.twitter.com/vMIv9eZARJ
March 19
If you wish to include mathematical expressions in a plot/text, but you are more comfortable writing them in LaTeX than in Rβs plotmath, you can use the {TeX} function from {latex2exp} π¦ for a translation! πhttps://t.co/cpyeNcFL1j#rstats #DataScience pic.twitter.com/gRPLIFljTa
β R Function A Day (@rfunctionaday) March 19, 2021
March 20
When you have to format or interpolate strings using piped data, you can do so easily using the {glue_data} function from {glue} π¦https://t.co/INusSE9k2u#rstats #DataScience pic.twitter.com/YGgl1ZAGNC
β R Function A Day (@rfunctionaday) March 20, 2021
March 21
If you wish to embed a well-formatted HTML table containing a summary of your regression model, the {tab_model} function from {sjPlot} π¦ can do so effortlessly:https://t.co/dtVUrfWVkB
β R Function A Day (@rfunctionaday) March 21, 2021
Supports simple, mixed-effects, Bayesian, etc. regression models.#rstats #DataScience pic.twitter.com/8wZha5ohNi
March 22
It can be a daunting task to construct regular expressions, especially complex ones.
β R Function A Day (@rfunctionaday) March 22, 2021
The {rx_} function family from {RVerbalExpressions} π¦ makes this more accessible by allowing one to construct regex using verbal expressions π‘https://t.co/5G77171gYA#rstats #DataScience pic.twitter.com/MbLmKSW0KX
March 23
If data has hierarchical categories, you may wish to see their breakdown into subcategories (e.g., to check if enough observations per cell for mixed-effects model).
β R Function A Day (@rfunctionaday) March 23, 2021
You can easily do so using {sizetree} function from {plotrix} π¦!https://t.co/tH5hUetZ2A#rstats #DataScience pic.twitter.com/5d52tK8Mx7
March 24
If you have a list-of-lists, sometimes you may wish to turn it inside out, i.e.Β convert a pair of lists into a list of pairs. π
β R Function A Day (@rfunctionaday) March 24, 2021
The {transpose} function from {purrr} π¦ makes this easy:https://t.co/TBQHC2jw3r#rstats #DataScience pic.twitter.com/7wS8by9ysf
March 25
While writing functions, either for a π¦ or for colleagues, you can construct an elegant user-interface (UI) using {ui_} function family from {usethis} π¦https://t.co/PxL3FRV8Qk
β R Function A Day (@rfunctionaday) March 25, 2021
Such UI provides helpful info, esp.Β for functions that do multiple things.#rstats #DataScience pic.twitter.com/kCgSLY9gMz
March 26
When you are visualizing data, in addition to plotting the full dataset, you may also wish to emphasize/zoom in on its subset for a special consideration.
β R Function A Day (@rfunctionaday) March 26, 2021
You can do this easily with the {facet_zoom} function from {ggforce} π¦! πhttps://t.co/Dm7Lbzv0Pt#rstats #DataScience pic.twitter.com/3EKrTafXRi
March 27
If in the middle of your piped workflow, you wish to access a side effect (e.g., plot or print something) and then continue on with chained operations, you can do so using the tee pipe operator {%T>%} from {magrittr} π¦ ! https://t.co/ibga7yyf5X#rstats #DataScience pic.twitter.com/5H0MoPOERY
β R Function A Day (@rfunctionaday) March 27, 2021
March 28
For correlation hypotheses, in addition to the joint distribution in a scatterplot, one may also wish to visualize the marginal distributions for each variable.
β R Function A Day (@rfunctionaday) March 28, 2021
This is easy to do using the {ggMarginal} function from {ggExtra} π¦!https://t.co/OgS1XsmY1U#rstats #DataScience pic.twitter.com/wE8mmJwnIx
March 29
If you need to convert a vector or a list into a dataframe, you can use the {enframe} function from {tibble} π¦ to do this conveniently:https://t.co/dsjo0w5aYD
β R Function A Day (@rfunctionaday) March 29, 2021
Compare its behavior with that of {data.frame} function to see when you want to prefer it.#rstats #DataScience pic.twitter.com/BxCBR64XF8
March 30
Sometimes you need to display two plots, not in a grid, but one inside the other (aka "inset" plot).
β R Function A Day (@rfunctionaday) March 30, 2021
Assuming you are using the grammar of graphics approach, this is easy to do using {annotation_custom} function from {ggplot2} π¦:https://t.co/GTV3UX2XV6#rstats #DataScience pic.twitter.com/cVH9J0dHTJ
March 31
If you need to pad strings, the appropriately named {str_pad} function from {stringr} π¦ makes this effortless π§΅https://t.co/Ar7LehWFQl
β R Function A Day (@rfunctionaday) March 31, 2021
See examples below to see all the different ways in which you can do such padding.#rstats #DataScience pic.twitter.com/TZpXJSWvwv