The following question seems very basic in programming with data.table, so my apologies if it's a duplicate. I spent time researching but could not find an answer.
I want to create a "user-defined function" that wraps around a data.table wrangling procedure. In this procedure, a new column is created, and I want to let the user set the name of that new column.
Example
Consider the following code that works as-is. I want to wrap it inside a function.
library(data.table)
library(magrittr)
library(tibble)
mtcars %>%
as.data.table() %>%
.[, .(max_mpg = max(mpg)), by = cyl] %>%
as_tibble()
#> # A tibble: 3 x 2
#> cyl max_mpg
#> <dbl> <dbl>
#> 1 6 21.4
#> 2 4 33.9
#> 3 8 19.2
Created on 2021-10-13 by the reprex package (v0.3.0)
All I want my function to do is let the user set the name of new_colname_of_choice:
my_wrapper <- function(new_colname_of_choice) {
mtcars %>%
as.data.table() %>%
.[, .(new_colname_of_choice = max(mpg)), by = cyl] %>%
as_tibble()
}
my_wrapper(new_colname_of_choice = "my_lovely_colname")
#> # A tibble: 3 x 2
#> cyl new_colname_of_choice <---------- why this isn't called "my_lovely_colname"?
#> <dbl> <dbl>
#> 1 6 21.4
#> 2 4 33.9
#> 3 8 19.2
I've tried using curly braces which didn't work either (actually threw an error):
my_wrapper_2 <- function(new_colname_of_choice) {
mtcars %>%
as.data.table() %>%
.[, .({new_colname_of_choice} = max(mpg)), by = cyl] %>%
as_tibble()
}
Error: unexpected '=' in:
" as.data.table() %>%
.[, .({new_colname_of_choice} ="
Which is surprising because curly braces do promote the desired naming ability, but in a different (yet similar) kind of code:
my_wrapper_3 <- function(new_colname_of_choice) {
mtcars %>%
as.data.table() %>%
.[, {new_colname_of_choice} := max(mpg), by = cyl] %>%
as_tibble()
}
my_wrapper_3(new_colname_of_choice = "my_lovely_colname")
## # A tibble: 32 x 12
## mpg cyl disp hp drat wt qsec vs am gear carb my_lovely_colname <---- SUCCESS!
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4 21.4
## 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4 21.4
## 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 33.9
## 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 21.4
## 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 19.2
## 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 21.4
## 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 19.2
## 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 33.9
## 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 33.9
## 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 21.4
## # ... with 22 more rows
Bottom line
My conclusion is that the = operator is sensitive to {...} on the LHS. How can I otherwise pass a name (from argument) to the LHS in the initial my_wrapper() example?
EDIT
I'd like to add the dplyr solution for the same problem, taken from the programming with dplyr vignette:
library(dplyr)
my_wrapper_dplyr <- function(new_colname_of_choice) {
mtcars %>%
group_by(cyl) %>%
summarise("{new_colname_of_choice}" := max(mpg))
}
my_wrapper_dplyr("another_lovely_colname")
Which is pretty robust and works in all naming situations I've encountered. Is there a built-in/canonical practice in data.table similar to {dplyr}'s?
With the upcoming data.table version 1.14.3, you'll be able to use the new env parameter:
A new interface for programming on data.table has been added, closing #2655 and many other linked issues. It is built using base R's substitute-like interface via a new env argument to [.data.table. For details see the new vignette programming on data.table, and the new ?substitute2 manual page. Thanks to numerous users for filing requests, and Jan Gorecki for implementing.
# install dev version
install.packages("https://github.com/Rdatatable/data.table/archive/master.tar.gz", repo = NULL, type = "source")
library(tibble)
library(data.table)
my_wrapper_new <- function(new_colname_of_choice) {
mtcars %>%
as.data.table() %>%
.[, .(new_colname_of_choice = max(mpg)), by = cyl,
env=list(new_colname_of_choice = new_colname_of_choice)] %>%
as_tibble()
}
my_wrapper_new('test')
# A tibble: 3 x 2
cyl test
<dbl> <dbl>
1 6 21.4
2 4 33.9
3 8 19.2
One thing you can do is separate the creation of the column and the naming of the column like so:
my_wrapper <- function(new_colname_of_choice) {
mtcars %>%
as.data.table() %>%
.[, .(tempcol = max(mpg)), by = cyl] %>%
setnames(., "tempcol", new_colname_of_choice) %>%
as.tibble()
}
my_wrapper("my_lovely_colname")
Using this method you can use either .(tempcol = max(mpg)) or tempcol := max(mpg)
Using setNames from stats:
my_wrapper <- function(new_colname_of_choice) {
mtcars %>%
as.data.table() %>%
.[, setNames(list(max(mpg)), new_colname_of_choice), by = cyl] %>%
as_tibble()
}
my_wrapper(new_colname_of_choice = "my_lovely_colname")
Related
I have a nested object whereby the name of individual vehicles in the inner nest. This is not my dataset but I can reproduce the error with mtcars. Essentially, I am trying to grab the manufacturing_size for the latest year when its anything but Not Provided, and use only this value for manufacturing_size. However, for whatever reason the map/function does not enter all nests.
dataset:
mtcars <- mtcars %>% rownames_to_column()
emp <- c("Not Provided","Less than 250","250 to 499","500 to 999","1000 to 4999","5000 to 19,999")
mtcars$manufacturing_size <- c(rep(emp, 5) , "Not Provided", "Less than 250")
mtcars$year <- rep(2018:2021, 8)
mtcars1 <- mtcars
mtcars2 <- mtcars
mtcars3 <- mtcars
mtcars1$year <- rep(c(2019:2021, 2018), 8)
mtcars2$year <- rep(c(2020:2021, 2018, 2019), 8)
mtcars3$year <- rep(c(2021:2018), 8)
mtcarsAll <- rbind(mtcars, mtcars1, mtcars2, mtcars3)
Here is what I have tried:
mtcars %>% nest_by(gear) %>% ungroup %>% mutate(data = map(data, ~ .x %>% nest(data=rowname) %>%
mutate(data = map(data, function(x){
someSize <- x[x$year == x[which.max(x$year),]$year,]$manufacturing_size
if(someSize != 'Not Provided'){
x$manufacturing_size = someSize
return(x)
}else {
for(i in 1:nrow(x)){
if(x$year[i] != 2018){
someSize <- x[x$year == x[which.max(x$year)-i,]$year,]$manufacturing_size
if(someSize != 'Not Provided'){
x$manufacturing_size = someSize
return(x)
}
} else{
someSize <- x[x$year == x[which.max(x$year)+i,]$year,]$manufacturing_size
if(someSize != 'Not Provided'){
x$manufacturing_size = someSize
return(x)
}
}
}
}
}
))))
Which produces the following error:
Error in `mutate()`:
! Problem while computing `data = map(...)`.
Caused by error in `mutate()`:
! Problem while computing `data = map(...)`.
Caused by error in `vectbl_as_row_location()`:
! Must subset rows with a valid subscript vector.
ℹ Logical subscripts must match the size of the indexed input.
✖ Input has size 1 but subscript `x$year == x[which.max(x$year)]$year` has size 0.
This is because If I remove most of the function and print out someSize then It enters the first outer nest but not the others. What is an easier alternative?
Using the answer below, the following works:
mtr <- mtcarsAll %>% group_by(rowname) %>%
mutate(
man_size = case_when(
manufacturing_size != "Not Provided" & max(year) == year~ manufacturing_size
)
)
mtr %>% ungroup %>%
fill(man_size, .direction = "updown")
Does this do what you want. There is a lot of nesting in your example, which unless I am mistaken, isn't necessary.
I've altered your setup a little bit cause I don't think what you wanted was going to work:
used mtcars2 so as to not overwrite mtcars,
replace rep(emp, 5) with random draws from a standard normal distrubution rnorm(30)) becuase you didn't define emp
added a new grouping variable group so that each year only appears once for each group. (The way you had it with gear as the grouping var didn't work because there were multiple values for the most recent year)
mtcars2 <- mtcars %>% rownames_to_column("make")
mtcars2$manufacturing_size <- c(rnorm(30),"Not Provided", "Less than 250")
mtcars2$group <- rep(LETTERS[1:8], each = 4)
mtcars2$year <- rep(2018:2021, 8)
Then, rather than all the complex nesting you've done, you just do use an if_else statement or, I've prefered case_when to get the values you are intereseted in for the new variable man_size.
mtcars2 %>%
group_by(group) %>%
mutate(
man_size = case_when(
manufacturing_size != "Not Provided" & max(year) == year ~ manufacturing_size,
TRUE ~ NA_character_
)
)
# A tibble: 32 × 16
# Groups: group [8]
make mpg cyl disp hp drat wt qsec vs am gear carb manufacturing_size group year man_size
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr> <int> <chr>
1 Mazda RX4 21 6 160 110 3.9 2.62 16.5 0 1 4 4 -0.10777645987017 A 2018 NA
2 Mazda RX4 Wag 21 6 160 110 3.9 2.88 17.0 0 1 4 4 0.685034939673918 A 2019 NA
3 Datsun 710 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1 0.0216291773402855 A 2020 NA
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1 0.227610843395319 A 2021 0.2276108433953…
5 Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2 0.342964251360947 B 2018 NA
6 Valiant 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1 1.20792448510301 B 2019 NA
7 Duster 360 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4 0.395983818669596 B 2020 NA
8 Merc 240D 24.4 4 147. 62 3.69 3.19 20 1 0 4 2 -0.42502805147035 B 2021 -0.425028051470…
9 Merc 230 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2 0.961054295375392 C 2018 NA
10 Merc 280 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4 -1.32030765978216 C 2019 NA
# … with 22 more rows
If you then want to fill in those NAs with what you need you can just use tidyr::fill
Hope this helps.
EDIT after change from OP in comments.
OK, I see what you want now. Thanks for providing emp. I still made one more tiny change to your setup, to ensure there was a case where Not Provided would be the value of manufacuring_size for the maximum year in one of the groups (for group H).
mtcars2 <- mtcars %>% rownames_to_column()
emp <- c("Not Provided","Less than 250","250 to 499","500 to 999","1000 to 4999","5000 to 19,999")
mtcars2$manufacturing_size <- c(rep(emp, 5) , "Less than 250", "Not Provided")
mtcars2$group <- rep(LETTERS[1:8], each = 4)
mtcars2$year <- rep(2018:2021, 8)
We can then use the following:
mtcars3 <- mtcars2 %>%
group_by(group) %>%
mutate(
man_size = case_when(
max(year[manufacturing_size != "Not Provided"]) == year ~ manufacturing_size,
TRUE ~ NA_character_
)
)
Then if you want to fill in all the values, you can do:
mtcars3 %>%
fill(man_size, .direction = "updown")
I'm trying to group_by multiple columns in my data frame and I can't write out every single column name in the group_by function so I want to call the column names as a vector like so:
cols <- grep("[a-z]{3,}$", colnames(mtcars), value = TRUE)
mtcars %>% filter(disp < 160) %>% group_by(cols) %>% summarise(n = n())
This returns error:
Error in mutate_impl(.data, dots) :
Column `mtcars[colnames(mtcars)[grep("[a-z]{3,}$", colnames(mtcars))]]` must be length 12 (the number of rows) or one, not 7
I definitely want to use a dplyr function to do this, but can't figure this one out.
Update
group_by_at() has been superseded; see https://dplyr.tidyverse.org/reference/group_by_all.html. Refer to Harrison Jones' answer for the current recommended approach.
Retaining the below approach for posterity
You can use group_by_at, where you can pass a character vector of column names as group variables:
mtcars %>%
filter(disp < 160) %>%
group_by_at(cols) %>%
summarise(n = n())
# A tibble: 12 x 8
# Groups: mpg, cyl, disp, drat, qsec, gear [?]
# mpg cyl disp drat qsec gear carb n
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
# 1 19.7 6 145.0 3.62 15.50 5 6 1
# 2 21.4 4 121.0 4.11 18.60 4 2 1
# 3 21.5 4 120.1 3.70 20.01 3 1 1
# 4 22.8 4 108.0 3.85 18.61 4 1 1
# ...
Or you can move the column selection inside group_by_at using vars and column select helper functions:
mtcars %>%
filter(disp < 160) %>%
group_by_at(vars(matches('[a-z]{3,}$'))) %>%
summarise(n = n())
# A tibble: 12 x 8
# Groups: mpg, cyl, disp, drat, qsec, gear [?]
# mpg cyl disp drat qsec gear carb n
# <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
# 1 19.7 6 145.0 3.62 15.50 5 6 1
# 2 21.4 4 121.0 4.11 18.60 4 2 1
# 3 21.5 4 120.1 3.70 20.01 3 1 1
# 4 22.8 4 108.0 3.85 18.61 4 1 1
# ...
I believe group_by_at has now been superseded by using a combination of group_by and across. And summarise has an experimental .groups argument where you can choose how to handle the grouping after you create a summarised object. Here is an alternative to consider:
cols <- grep("[a-z]{3,}$", colnames(mtcars), value = TRUE)
original <- mtcars %>%
filter(disp < 160) %>%
group_by_at(cols) %>%
summarise(n = n())
superseded <- mtcars %>%
filter(disp < 160) %>%
group_by(across(all_of(cols))) %>%
summarise(n = n(), .groups = 'drop_last')
all.equal(original, superseded)
Here is a blog post that goes into more detail about using the across function:
https://www.tidyverse.org/blog/2020/04/dplyr-1-0-0-colwise/
I'm trying to write a custom function that will compute a new variable based on values from a predefined vector of variables (e.g., vector_heavy) and then name the new variable based on an argument provided to the function (e.g., custom_name).
This variable naming is where my quosure skills are failing me. Any help is greatly appreciated.
library(tidyverse)
vector_heavy <- quos(disp, wt, cyl)
cv_compute <- function(data, cv_name, cv_vector){
cv_name <- enquo(cv_name)
data %>%
rowwise() %>%
mutate(!!cv_name = mean(c(!!!cv_vector), na.rm = TRUE)) %>%
ungroup()
}
d <- cv_compute(mtcars, cv_name = custom_name, cv_vector = vector_heavy)
My error message reads:
Error: unexpected '=' in:
" rowwise() %>%
mutate(!!cv_name ="
Removing the !! before cv_name within mutate() will result in a function that calculates a new variable literally named cv_name, and ignoring the custom_name I've included as an argument.
cv_compute <- function(data, cv_name, cv_vector){
cv_name <- enquo(cv_name)
data %>%
rowwise() %>%
mutate(cv_name = mean(c(!!!cv_vector), na.rm = TRUE)) %>%
ungroup()
}
How can I get this function to utilize the custom_name I supply as an argument for cv_name?
You need to use the := helper within mutate. You'll also need quo_name to convert the input to a string.
The mutate line of your function will then look like
mutate(!!quo_name(cv_name) := mean(c(!!!cv_vector), na.rm = TRUE))
In its entirety:
cv_compute <- function(data, cv_name, cv_vector){
cv_name <- enquo(cv_name)
data %>%
rowwise() %>%
mutate(!!quo_name(cv_name) := mean(c(!!!cv_vector), na.rm = TRUE)) %>%
ungroup()
}
cv_compute(mtcars, cv_name = custom_name, cv_vector = vector_heavy)
mpg cyl disp hp drat wt qsec vs am gear carb custom_name
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 56.20667
2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 56.29167
3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 38.10667
4 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 89.07167
5 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 123.81333
6 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 78.15333
I am grouping data and then summarizing it, but would also like to retain another column. I do not need to do any evaluations of that column's content as it will always be the same as the group_by column. I can add it to the group_by statement but that does not seem "right". I want to retain State.Full.Name after grouping by State. Thanks
TDAAtest <- data.frame(State=sample(state.abb,1000,replace=TRUE))
TDAAtest$State.Full.Name <- state.name[match(TDAAtest$State,state.abb)]
TDAA.states <- TDAAtest %>%
filter(!is.na(State)) %>%
group_by(State) %>%
summarize(n=n()) %>%
ungroup() %>%
arrange(State)
Perhaps we need
TDAAtest %>%
filter(!is.na(State)) %>%
group_by(State) %>%
summarise(State.Full.Name = first(State.Full.Name), n = n())
Or use mutate to create the column and then do the distinct
TDAAtest %>% f
filter(!is.na(State)) %>%
group_by(State) %>%
mutate(n= n()) %>%
distinct(State, .keep_all=TRUE)
To retain all columns, you can include across() as a summarize argument, as explained in the documentation for dplyr::do().
by_cyl <- head(mtcars) %>%
group_by(cyl)
by_cyl %>%
summarise(m_mpg = mean(mpg), across())
cyl m_mpg mpg disp hp drat wt qsec vs am gear carb
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 4 22.8 22.8 108 93 3.85 2.32 18.6 1 1 4 1
2 6 20.4 21 160 110 3.9 2.62 16.5 0 1 4 4
3 6 20.4 21 160 110 3.9 2.88 17.0 0 1 4 4
4 6 20.4 21.4 258 110 3.08 3.22 19.4 1 0 3 1
5 6 20.4 18.1 225 105 2.76 3.46 20.2 1 0 3 1
6 8 18.7 18.7 360 175 3.15 3.44 17.0 0 0 3 2
To retain only a subset of unaltered columns, you can select them within across using tidyselect semantics.
I believe there are more accurate answers than the accepted answer specially when you don't have unique data for other columns in each group (e.g. max or min or top n items based on one particular column
).
Although the accepted answer works for this question, for instance, you would like to find the county with the max population for each state. (You need to have county and population columns).
We have the following options:
1. dplyr version
From this link, you have three extra operations (mutate, ungroup and filter) to achieve that:
TDAAtest %>%
filter(!is.na(State)) %>%
group_by(State) %>%
mutate(maxPopulation = max(Population)) %>%
ungroup() %>%
filter(maxPopulation == Population)
2. Function version
This one gives you as much flexibility as you want and you can apply any kind of operation to each group:
maxFUN = function(x) {
# order population in a descending order
x = x[with(x, order(-Population)), ]
x[1, ]
}
TDAAtest %>%
filter(!is.na(State)) %>%
group_by(State) %>%
do(maxFUN(.))
This one is highly recommended for more complex operations. For instance, you can return top n (topN) counties per state by having x[1:topN] for the returned dataframe in maxFUN.
Is there a way to output the result of a pipeline at each step without doing it manually? (eg. without selecting and running only the selected chunks)
I often find myself running a pipeline line-by-line to remember what it was doing or when I am developing some analysis.
For example:
library(dplyr)
mtcars %>%
group_by(cyl) %>%
sample_frac(0.1) %>%
summarise(res = mean(mpg))
# Source: local data frame [3 x 2]
#
# cyl res
# 1 4 33.9
# 2 6 18.1
# 3 8 18.7
I'd to select and run:
mtcars %>% group_by(cyl)
and then...
mtcars %>% group_by(cyl) %>% sample_frac(0.1)
and so on...
But selecting and CMD/CTRL+ENTER in RStudio leaves a more efficient method to be desired.
Can this be done in code?
Is there a function which takes a pipeline and runs/digests it line by line showing output at each step in the console and you continue by pressing enter like in demos(...) or examples(...) of package guides
You can select which results to print by using the tee-operator (%T>%) and print(). The tee-operator is used exclusively for side-effects like printing.
# i.e.
mtcars %>%
group_by(cyl) %T>% print() %>%
sample_frac(0.1) %T>% print() %>%
summarise(res = mean(mpg))
It is easy with magrittr function chain. For example define a function my_chain with:
foo <- function(x) x + 1
bar <- function(x) x + 1
baz <- function(x) x + 1
my_chain <- . %>% foo %>% bar %>% baz
and get the final result of a chain as:
> my_chain(0)
[1] 3
You can get a function list with functions(my_chain)
and define a "stepper" function like this:
stepper <- function(fun_chain, x, FUN = print) {
f_list <- functions(fun_chain)
for(i in seq_along(f_list)) {
x <- f_list[[i]](x)
FUN(x)
}
invisible(x)
}
And run the chain with interposed print function:
stepper(my_chain, 0, print)
# [1] 1
# [1] 2
# [1] 3
Or with waiting for user input:
stepper(my_chain, 0, function(x) {print(x); readline()})
Add print:
mtcars %>%
group_by(cyl) %>%
print %>%
sample_frac(0.1) %>%
print %>%
summarise(res = mean(mpg))
IMHO magrittr is mostly useful interactively, that is when I am exploring data or building a new formula/model.
In this cases, storing intermediate results in distinct variables is very time consuming and distracting, while pipes let me focus on data, rather than typing:
x %>% foo
## reason on results and
x %>% foo %>% bar
## reason on results and
x %>% foo %>% bar %>% baz
## etc.
The problem here is that I don't know in advance what the final pipe will be, like in #bergant.
Typing, as in #zx8754,
x %>% print %>% foo %>% print %>% bar %>% print %>% baz
adds to much overhead and, to me, defeats the whole purpose of magrittr.
Essentially magrittr lacks a simple operator that both prints and pipes results.
The good news is that it seems quite easy to craft one:
`%P>%`=function(lhs, rhs){ print(lhs); lhs %>% rhs }
Now you can print an pipe:
1:4 %P>% sqrt %P>% sum
## [1] 1 2 3 4
## [1] 1.000000 1.414214 1.732051 2.000000
## [1] 6.146264
I found that if one defines/uses a key bindings for %P>% and %>%, the prototyping workflow is very streamlined (see Emacs ESS or RStudio).
I wrote the package pipes that can do several things that might help :
use %P>% to print the output.
use %ae>% to use all.equal on input and output.
use %V>% to use View on the output, it will open a viewer for each relevant step.
If you want to see some aggregated info you can try %summary>%, %glimpse>% or %skim>% which will use summary, tibble::glimpse or skimr::skim, or you can define your own pipe to show specific changes, using new_pipe
# devtools::install_github("moodymudskipper/pipes")
library(dplyr)
library(pipes)
res <- mtcars %P>%
group_by(cyl) %P>%
sample_frac(0.1) %P>%
summarise(res = mean(mpg))
#> group_by(., cyl)
#> # A tibble: 32 x 11
#> # Groups: cyl [3]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
#> 2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
#> 3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
#> 4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
#> 5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> 6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
#> 7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
#> 8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
#> 9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
#> 10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
#> # ... with 22 more rows
#> sample_frac(., 0.1)
#> # A tibble: 3 x 11
#> # Groups: cyl [3]
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 26 4 120. 91 4.43 2.14 16.7 0 1 5 2
#> 2 17.8 6 168. 123 3.92 3.44 18.9 1 0 4 4
#> 3 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
#> summarise(., res = mean(mpg))
#> # A tibble: 3 x 2
#> cyl res
#> <dbl> <dbl>
#> 1 4 26
#> 2 6 17.8
#> 3 8 18.7
res <- mtcars %ae>%
group_by(cyl) %ae>%
sample_frac(0.1) %ae>%
summarise(res = mean(mpg))
#> group_by(., cyl)
#> [1] "Attributes: < Names: 1 string mismatch >"
#> [2] "Attributes: < Length mismatch: comparison on first 2 components >"
#> [3] "Attributes: < Component \"class\": Lengths (1, 4) differ (string compare on first 1) >"
#> [4] "Attributes: < Component \"class\": 1 string mismatch >"
#> [5] "Attributes: < Component 2: Modes: character, list >"
#> [6] "Attributes: < Component 2: Lengths: 32, 2 >"
#> [7] "Attributes: < Component 2: names for current but not for target >"
#> [8] "Attributes: < Component 2: Attributes: < target is NULL, current is list > >"
#> [9] "Attributes: < Component 2: target is character, current is tbl_df >"
#> sample_frac(., 0.1)
#> [1] "Different number of rows"
#> summarise(., res = mean(mpg))
#> [1] "Cols in y but not x: `res`. "
#> [2] "Cols in x but not y: `qsec`, `wt`, `drat`, `hp`, `disp`, `mpg`, `carb`, `gear`, `am`, `vs`. "
res <- mtcars %V>%
group_by(cyl) %V>%
sample_frac(0.1) %V>%
summarise(res = mean(mpg))
# you'll have to test this one by yourself