Member-only story

Quick Take: How to Use The Across Function in R Programming

Pierre DeBois
3 min readOct 26, 2022

--

(Update as of November 19th, 2023: I incorporated the correction mentioned in the comments from Paul Jacobson. Special thanks to Paul for catching my oversight.)

If you are starting out in R and data science, you will find functions that become your go-to tactics in code creation. One function that will likely get the most keyboard action among data scientists and analysts is the across() function. The across() function is designed to apply a calculation or function on one or several columns at once.

The across() function was created to “replace” three variants of condition that is applied to several functions — ***_if(), ***_at() and ***_all(). The variants are used with the mutate and summarise functions. I type “replace” because these variants are still used in R programming code — you can still look these up online to see their usage. The if/at/all variants were meant to apply conditions for a column in a vector, matrix, data frame, or data table. These days you will likely see the across() function used in a current example because it introduces easier programming decisions in many instances.

The best way of thinking of the across() function is to use it with the mutate function (I explain the mutate() function in a separate post). Here is an example:

I am importing a dataset of bicycle data — this is from Kaggle, representing cycle usage in Seoul. I am interested in the temperature and dew point temperatures. The names are…

--

--

Pierre DeBois
Pierre DeBois

Written by Pierre DeBois

#analytics |#datascience |#JS |#rstats |#marketing services for #smallbiz | #retail | #nonprofits Contrib @CMSWire @smallbiztrends #blackbusiness #BLM

No responses yet