dplyr::n() returns “Error: Error: n() should only be called in a data context ”

后端 未结 2 402
礼貌的吻别
礼貌的吻别 2020-12-04 01:34

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(         


        
2条回答
  •  青春惊慌失措
    2020-12-04 02:16

    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"    
    

提交回复
热议问题