sample_frac function of dplyr is giving back an error

Hi *,

I would be very grateful for your help in solving this problem. I want to:

  1. understand why that happened
  2. solve that problem and using sample_frac function of dplyr.

I'm using
sample_frac(ecom, size = 0.7)

  • Expected result
    Load 70% of my data of the ecom dataset.

  • Given result

Error: n() should only be called in a data context
Call rlang::last_error() to see a backtrace

  • Error trace

rlang::last_trace()
x

  1. +-dplyr::sample_frac(ecom, size = 0.7)
  2. +-dplyr:::sample_frac.data.frame(ecom, size = 0.7)
  3. | +-dplyr::slice(...)
  4. | -dplyr:::slice.tbl_df(...)
  5. | -dplyr:::slice_impl(.data, quo)
  6. +-sample.int(...)
  7. +-base::sample.int(...)
  8. -dplyr::n()
  9. -dplyr:::from_context("..group_size")
  10. \-`%||%`(...)  
    
  • 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

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 reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

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)

  • Result:

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

  • The trace says:

rlang::last_trace()
x

  1. +-dplyr::sample_frac(myDF, size = 0.7)
  2. +-dplyr:::sample_frac.data.frame(myDF, size = 0.7)
  3. | +-dplyr::slice(...)
  4. | -dplyr:::slice.data.frame(...)
  5. | +-base::as.data.frame(slice(tbl_df(.data), ..., .preserve = .preserve))
  6. | +-dplyr::slice(tbl_df(.data), ..., .preserve = .preserve)
  7. | -dplyr:::slice.tbl_df(tbl_df(.data), ..., .preserve = .preserve)
  8. | -dplyr:::slice_impl(.data, quo)
  9. +-sample.int(...)
  10. +-base::sample.int(...)
  11. -dplyr::n()
  12. -dplyr:::from_context("..group_size")
  13. \-`%||%`(...)

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

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.