Hi *,
I would be very grateful for your help in solving this problem. I want to:
understand why that happened
solve that problem and using sample_frac function of dplyr.
I'm using
sample_frac(ecom, size = 0.7)
Error: n() should only be called in a data context
Call rlang::last_error()
to see a backtrace
rlang::last_trace()
x
+-dplyr::sample_frac(ecom, size = 0.7)
+-dplyr:::sample_frac.data.frame(ecom, size = 0.7)
| +-dplyr::slice(...)
| -dplyr:::slice.tbl_df(...)
| -dplyr:::slice_impl(.data, quo)
+-sample.int(...)
+-base::sample.int(...)
-dplyr::n()
-dplyr:::from_context("..group_size")
\-`%||%`(...)
Session info
#> R version 3.5.1 (2018-07-02)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 17134)
#>
#> Matrix products: default
#>
#> locale:
#> [1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252
#> [3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C
#> [5] LC_TIME=German_Germany.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] readr_1.3.1 dplyr_0.8.3
#>
#>
#> loaded via a namespace (and not attached):
#> [1] compiler_3.5.1 magrittr_1.5 tools_3.5.1 htmltools_0.3.6
#> [5] yaml_2.2.0 Rcpp_1.0.1 stringi_1.4.3 rmarkdown_1.14
#> [9] highr_0.8 knitr_1.23 stringr_1.4.0 xfun_0.8
#> [13] digest_0.6.20 evaluate_0.14
FJCC
July 21, 2019, 8:32pm
2
What is your variable ecom? sample_frac works fine for a data frame.
df <- data.frame(X = seq(1:100), Z = seq(201:300))
SMPL <- dplyr::sample_frac(df, size = 0.7)
nrow(SMPL)
#> [1] 70
Created on 2019-07-21 by the reprex package (v0.2.1)
ecom is the name of my dataset I want apply the function sample_frac
To help us help you, could you please prepare a repr oducible ex ample (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
Sorry: I forgot to mention the type of the object ecom:
typeof(ecom)
[1] "list"
My first idea was it's not working because it's a list.
So I converted ecom into a dataframe by using:
myDF <- as.data.frame(ecom)
class(myDF)
[1] "data.frame"
But the error stays existing:
sample_frac(myDF, size = 0.7)
Error: n() should only be called in a data context
rlang::last_trace()
x
+-dplyr::sample_frac(myDF, size = 0.7)
+-dplyr:::sample_frac.data.frame(myDF, size = 0.7)
| +-dplyr::slice(...)
| -dplyr:::slice.data.frame(...)
| +-base::as.data.frame(slice(tbl_df(.data), ..., .preserve = .preserve))
| +-dplyr::slice(tbl_df(.data), ..., .preserve = .preserve)
| -dplyr:::slice.tbl_df(tbl_df(.data), ..., .preserve = .preserve)
| -dplyr:::slice_impl(.data, quo)
+-sample.int(...)
+-base::sample.int(...)
-dplyr::n()
-dplyr:::from_context("..group_size")
\-`%||%`(...)
FJCC
July 21, 2019, 10:21pm
7
As requested by andresrcs, please make a reproducible example of your problem. Make a data frame from the first few rows of myDF, showing the data explicitly, and run sample_frac on that, showing the result.
Issue is solved:
After detach all packages except datasets, dplyr and base I had quit the Rsession. After opened a new Rsession and reload all packages I had before the error occured it ran properly:
sample_frac(ecom, size = 0.01)
# A tibble: 10 x 8
referrer device n_visit n_pages duration purchase order_items order_value
<fct> <fct> <dbl> <dbl> <dbl> <lgl> <dbl>
1 google tablet 10 1 905 FALSE 0 0
2 direct tablet 10 18 324 TRUE 10 1497
3 google tablet 1 1 73 FALSE 0 0
4 google laptop 0 3 45 FALSE 0 0
5 yahoo mobile 8 4 112 FALSE 7 621
6 social mobile 10 17 459 FALSE 0 0
7 bing tablet 3 1 134 FALSE 0 0
8 bing tablet 0 3 42 FALSE 0 0
9 google tablet 9 1 382 FALSE 0 0
10 google laptop 5 9 261 TRUE 6 2086
system
Closed
July 28, 2019, 11:04pm
9
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.