I got for the following code
for (i in c(1:(ncol(df_multi_paths_cols) - 1))) {
df_cache <- df_multi_paths_cols %>%
select(num_range("ord_", c(i, i+1))) %>%
#select within dataset columns with prefix and within specific range i and i+1
na.omit() %>%
# The na.omit R function removes all incomplete cases of a data object
# (typically of a data frame, matrix or vector).
group_by(.dots = c(paste0("ord_", c(i, i+1)))) %>%
#paste=concatenate strings without separator
# group_by() takes an existing tbl and converts it into a grouped tbl where
# operations are performed "by group". ungroup() removes grouping.
summarise(number = n()) %>%
# summarise() is typically used on grouped data created by group_by(). The output will
# have one row for each group.
#n()=numbe robservation in the group
ungroup()
colnames(df_cache)[c(1, 2)] <- c('channel_from', 'channel_to')
df_res[[i]] <- df_cache
}
The below error:
Error: n() should only be called in a data context
Searched on the web but seems no solution has been described related to this specific error. Any hints more than appreciated as I'm not able to figure out why the n() function is not working within summarise.
The dataframe df_multi_path_cols is like this:
> df_multi_paths_cols
ord_1 ord_2 ord_3 ord_4
1 (start) MANAGER_SASWP MANAGER_SRSLT MANAGER_3RDWP
2 (start) GROUPDIRECTOR/CXO_LIVEWEBEXR GROUPDIRECTOR/CXO_SASWEB GROUPDIRECTOR/CXO_WPR
3 (start) GROUPDIRECTOR/CXO_SUG (null) <NA>
4 (start) GROUPDIRECTOR/CXO_SUG GROUPDIRECTOR/CXO_3RDWP GROUPDIRECTOR/CXO_SASLIVE
5 (start) GROUPDIRECTOR/CXO_SUG GROUPDIRECTOR/CXO_SUG (null)
6 (start) DIRECTOR_3RDLIVE DIRECTOR_ODSASWEBIN DIRECTOR_SUG
7 (start) DIRECTOR_DMCR DIRECTOR_SUG (null)
8 (start) DIRECTOR_3RDLIVE DIRECTOR_SUG GROUPDIRECTOR/CXO_SASWP
9 (start) DIRECTOR_SUG DIRECTOR_EPCR DIRECTOR_EPCR
10 (start) DIRECTOR_SUG DIRECTOR_SUG MANAGER_SASWP
11 (start) MANAGER_SUG (null) <NA>
ord_5 ord_6 ord_7 ord_8
1 MANAGER_SASWP MANAGER_EBOOK MANAGER_3RDWP MANAGER_ONASOFF
2 (conversion) <NA> <NA> <NA>
3 <NA> <NA> <NA> <NA>
4 GROUPDIRECTOR/CXO_WBR GROUPDIRECTOR/CXO_SUG GROUPDIRECTOR/CXO_WDLR GROUPDIRECTOR/CXO_WDLR
5 <NA> <NA> <NA> <NA>
6 DIRECTOR_ONASOFF DIRECTOR_WPR MANAGER_SUG (conversion)
Update 04/07/2019
dput(df_multi_paths_cols)
structure(list(ord_1 = c("(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)", "(start)" ), ord_2 = c("MANAGER_SASWP", "GROUPDIRECTOR/CXO_LIVEWEBEXR", "GROUPDIRECTOR/CXO_SUG", "GROUPDIRECTOR/CXO_SUG", "GROUPDIRECTOR/CXO_SUG", "DIRECTOR_3RDLIVE", "DIRECTOR_DMCR", "DIRECTOR_3RDLIVE", "DIRECTOR_SUG", "DIRECTOR_SUG", "MANAGER_SUG", "TEAMMEMBER_3RDLIVE", "TEAMMEMBER_3RDLIVE", "TEAMMEMBER_OTHR", "TEAMMEMBER_SASLIVE", "DIRECTOR_3RDLIVE", "DIRECTOR_SASWP", "DIRECTOR_WBR", "TEAMMEMBER_EBOOK", "TEACHER_DMCR", "TEACHER_DMCR", "TEACHER_OTHR", "TEACHER_RMCHR", "TEACHER_SUG", "TEACHER_WBR", "DIRECTOR_DMCR", "DIRECTOR_DMCR", "DIRECTOR_DMCR", "DIRECTOR_SASEXEC", "DIRECTOR_SASLIVE", "DIRECTOR_SUG", "DIRECTOR_SUG", "DIRECTOR_SUG", "TEAMMEMBER_3RDWEBIN", "_SASLIVE", "DIRECTOR_SUG", "TEAMMEMBER_3RDLIVE", "DIRECTOR_SASEXEC", "DIRECTOR_SUG", "MANAGER_TEL", "DIRECTOR_SUG", "DIRECTOR_3RDLIVE", "DIRECTOR_DMCR", "DIRECTOR_SASLIVE", "DIRECTOR_SASLIVE", "DIRECTOR_SUG", "DIRECTOR_WBR", "MANAGER_3RDLIVE", "MANAGER_SASLIVE", "MANAGER_SUG", "_CR", "DIRECTOR_SASLIVE", "DIRECTOR_3RDLIVE", "DIRECTOR_3RDLIVE", "TEAMMEMBER_SASLIVE", "DIRECTOR_SUG", "DIRECTOR_TEL", "MANAGER_3RDLIVE", "MANAGER_IKR", "MANAGER_SASEXEC", "MANAGER_SASLIVE", "MANAGER_SEFR", "MANAGER_TEL", "_CR", "_AR", "_WPR", "DIRECTOR_SASLIVE", "TEAMMEMBER_DMR", "TEAMMEMBER_ID", "TEAMMEMBER_IKR", "TEAMMEMBER_SASEXEC", "TEAMMEMBER_SASLIVE", "TEAMMEMBER_SEFR", "TEAMMEMBER_SRSLT", "TEAMMEMBER_TEL", "MANAGER_EBOOK", "MANAGER_SEFR", "DIRECTOR_COMR", "DIRECTOR_CR", "DIRECTOR_DMR", "DIRECTOR_TEL", "TEAMMEMBER_TEL", "DIRECTOR_DMR", "DIRECTOR_SASLIVE", "DIRECTOR_SASLIVE", "DIRECTOR_SRSLT", "DIRECTOR_TEL", "DIRECTOR_TEL", "MANAGER_SASLIVE", "MANAGER_WPR", "MANAGER_WPR", "TEAMMEMBER_3RDLIVE", "TEAMMEMBER_Lead GenerationR", "TEAMMEMBER_SASLIVE", "TEAMMEMBER_COMR", "TEAMMEMBER_CR", "TEAMMEMBER_DMR", "TEAMMEMBER_IKR", "TEAMMEMBER_TEL", "DIRECTOR_IKR", "DIRECTOR_SASLIVE", "DIRECTOR_SASLIVE", "DIRECTOR_SASEXEC", "MANAGER_CS", "DIRECTOR_3RDLIVE", "DIRECTOR_RMCHR", "DIRECTOR_SASEXEC", "DIRECTOR_3RDLIVE", "DIRECTOR_SASWP", "MANAGER_CR", "DIRECTOR_IKR", "DIRECTOR_SASLIVE", "TEAMMEMBER_SASLIVE", "TEAMMEMBER_CR", "MANAGER_OTHR", "TEAMMEMBER_CR", "MANAGER_SRSLT", "DIRECTOR_DMR", "DIRECTOR_IKR", "MANAGER_COMR", "MANAGER_DMR", "MANAGER_IKR", "MANAGER_TEL", "DIRECTOR_SASLIVE", "MANAGER_3RDWP", "TEAMMEMBER_WS", "DIRECTOR_3RDLIVE", "DIRECTOR_SASLIVE", "_SASLIVE", "DIRECTOR_3RDLIVE", "TEAMMEMBER_3RDLIVE", "TEAMMEMBER_COMR", "TEAMMEMBER_WPR", "TEAMMEMBER_CR", "TEAMMEMBER_ARCHWEB", "TEAMMEMBER_ASKOD", "TEAMMEMBER_SASLIVE", "TEAMMEMBER_SASLIVE", "TEAMMEMBER_SASLIVE", "TEAMMEMBER_SASLIVE", "TEAMMEMBER_WS", "GROUPDIRECTOR/CXO_SASLIVE", "MANAGER_SASLIVE", "GROUPDIRECTOR/CXO_DMCR", "GROUPDIRECTOR/CXO_SASLIVE", "GROUPDIRECTOR/CXO_SEFR", "MANAGER_3RDLIVE",
The error disappeared by specifying for each function of the dplyr package the relative package of provenience:
for (i in c(1:(ncol(df_multi_paths_cols) - 1))) {
df_cache <- df_multi_paths_cols %>%
select(num_range("ord_", c(i, i+1))) %>%
na.omit() %>%
dplyr::group_by(.dots = c(paste0("ord_", c(i, i+1)))) %>%
dplyr::summarise(number=dplyr::n()) %>%
ungroup()
colnames(df_cache)[c(1, 2)] <- c('channel_from', 'channel_to')
df_res[[i]] <- df_cache
}
Pretty much now I think it was some kind of conflict with other packages with the same function name "summarise", as proved by
> conflicts()
[1] "predictors" "%>%" "compact" "lift" "cols" "%>%"
[7] "%>%" "shift" "take" "transpose" "%>%" "add_row"
[13] "arrange" "as_data_frame" "as_tibble" "between" "coalesce" "count"
[19] "data_frame" "data_frame_" "desc" "failwith" "first" "frame_data"
[25] "glimpse" "id" "last" "lst" "lst_" "mutate"
[31] "n" "rename" "summarise" "summarize" "tbl_sum" "tibble"
[37] "tribble" "trunc_mat" "type_sum" "matches" "%>%" "%>%"
[43] "expand" "expm" "t" "%>%" "enexpr" "enexprs"
[49] "enquo" "enquos" "ensym" "ensyms" "expr" "quo"
[55] "quo_name" "quos" "sym" "syms" "vars" "dcast"
[61] "melt" "smiths" "coerce" "plot" "print" "show"
[67] "summary" "t" "cov" "cov2cor" "df" "filter"
[73] "lag" "lowess" "predict" "smooth" "toeplitz" "update"
[79] "var" "image" "plot" "?" "data" "head"
[85] "tail" "Arith" "cbind2" "coerce" "Compare" "kronecker"
[91] "Logic" "Math" "Math2" "Ops" "rbind2" "show"
[97] "Summary" "all.equal" "as.array" "as.matrix" "body<-" "chol"
[103] "chol2inv" "colMeans" "colSums" "crossprod" "det" "determinant"
[109] "diag" "diag<-" "diff" "drop" "format" "intersect"
[115] "isSymmetric" "kronecker" "mean" "norm" "Position" "print"
[121] "qr" "qr.coef" "qr.fitted" "qr.Q" "qr.qty" "qr.qy"
[127] "qr.R" "qr.resid" "rcond" "rowMeans" "rownames" "rowSums"
[133] "setdiff" "setequal" "solve" "summary" "t" "tcrossprod"
[139] "union" "unname" "url" "which" "zapsmall"
This is because of the conflicts in packages, i.e., summarize/summarise is present in both the "dplyr" package and the "plyr" package. It is likely the function got called from the wrong package, resulting in this error. We can call the function while referencing its package to avoid such an issue.
To demonstrate:
# Call summarise from plyr library
plyr::summarise(count = n())
# Call summarise from dplyr library
dplyr::summarise(count = n())
I think this will help, let me know if you have any issue. Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With