Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I want ot the differnece between cur_data() and across(). The following is the case where using cur_data() and across() yields the same result:

library(tidyverse)

by_cyl <- mtcars %>% group_by(cyl)

a <- summarise(by_cyl,head(cur_data(), 2)) 

b <- summarise(by_cyl,head(across(), 2)))

identical(a,b)

#TRUE

1 Answer

0 votes
by (108k points)

I think you have understood the topic in the wrong way. Basically, the across() and cur_data() are not at all related or interchangeable because they both are for different purposes in R programming.

The across() is used to implement a function on multiple columns whereas the cur_data() returns the current data for a group without the grouping variable.

For example,

library(dplyr)

mtcars %>% summarise(across())

The above code returns the same mtcars data set as it is because the default values in across are .cols = everything() and .fns = NULL. So it implements the NULL function to all the columns.

Browse Categories

...