code: @Override public ValFrame apply(Env env, Env.StackHelp stk, AstRoot asts[]) {
      Frame fr = stk.track(asts[1].exec(env)).getFrame(); // first argument is dataframe
      int[] groupbycols = ((AstParameter)asts[2]).columns(fr.names());
      int[] sortcols =((AstParameter)asts[3]).columns(fr.names());  // sort columns
  
      int[] sortAsc;
      if (asts[4] instanceof AstNumList)
        sortAsc = ((AstNumList) asts[4]).expand4();
      else
        sortAsc = new int[]{(int) ((AstNum) asts[4]).getNum()};  // R client can send 1 element for some reason
      String newcolname = asts[5].str();
      Boolean sortColsOrder = ((AstNum) asts[6]).getNum()==1;
      
      assert sortAsc.length==sortcols.length;
      SortnGrouby sortgroupbyrank = new SortnGrouby(fr, groupbycols, sortcols, sortAsc, newcolname);
      sortgroupbyrank.doAll(sortgroupbyrank._groupedSortedOut);  // sort and add rank column
      RankGroups rankgroups = new RankGroups(sortgroupbyrank._groupedSortedOut, groupbycols,
              sortcols, sortgroupbyrank._chunkFirstG, sortgroupbyrank._chunkLastG,
              sortgroupbyrank._newRankCol).doAll(sortgroupbyrank._groupedSortedOut);
  
      if (sortColsOrder)
        return new ValFrame(rankgroups._finalResult.sort(sortcols, sortAsc));
      else
        return new ValFrame(rankgroups._finalResult);
    }
docs: (rank_within_groupby frame groupby_cols sort_cols sort_orders new_colname)