dplyr:在group_by和summary之后安排不按预期运行

时间:2015-09-07 22:44:20

标签: r dplyr

我必须遗漏dplyr mtcars %>% group_by( cyl, gear ) %>% summarize( hp_range = max(hp) - min(mpg)) %>% arrange( desc(hp_range) ) # Source: local data frame [8 x 3] # Groups: cyl [3] # # cyl gear hp_range # (dbl) (dbl) (dbl) #1 4 4 87.6 #2 4 5 87.0 #3 4 3 75.5 #4 6 5 155.3 #5 6 4 105.2 #6 6 3 91.9 #7 8 5 320.0 #8 8 3 234.6 hp_range被剥离的内容。在下面的示例中,我将2列分组,将值汇总到单个变量中,然后按该新变量排序:

desc

显然,这不按预期按var isolateBindings = Object.keys(scope.$$isolateBindings || {}); var watchIsValid = $scope.$watchGroup(isolateBindings, function() { console.log('do somthing'); }); 排序。我错过了什么?

编辑:该示例按预期工作,未安排调用<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> &nbsp; <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <asp:ListView ID="CourseListView" runat="server" DataKeyNames="ID" OnItemEditing="CourseListView_ItemEditing" OnItemUpdating="CourseListView_ItemUpdating" OnItemInserting="CourseListView_ItemInserting" OnItemCanceling="CourseListView_ItemCanceling" OnPagePropertiesChanging="PagePropertiesChanging" InsertItemPosition="LastItem" > <LayoutTemplate> <table class="table" runat="server" id="tblCourse"> <tr id="itemPlaceholder" class="info" runat="server"></tr> </table> </LayoutTemplate> <ItemTemplate> <tr runat="server"> <td> <asp:LinkButton ID="EditLink" runat="server" CommandName="Edit" Text="Edit" /> <asp:LinkButton ID="DeleteLink" runat="server" CommandName="Delete" Text="Delete" /> </td> </tr> <tr runat="server"> <td class="col-md-4"> CourseName: <asp:Label ID="CourseNameLabel" runat="server" Text='<%#Eval("CourseName") %>'/> <br /> Discription: <asp:Label ID="DescriptionLabel" runat="server" Text='<%#Eval("Description") %>'/> <br /> StartDate: <asp:Label ID="StartDateLabel" runat="server" Text='<%#Eval("StartDate") %>'/> <br /> EndDate: <asp:Label ID="EndDateLabel" runat="server" Text='<%#Eval("EndDate") %>' /> <br /> CourseMode: <asp:Label ID="CourseModeLabel" runat="server" Text='<%#Eval("CourseMode") %>'/> </td> </tr> </ItemTemplate> <EditItemTemplate> <tr runat="server"> <td> <asp:LinkButton ID="UpdateLink" runat="server" CommandName="Update" Text="Update" /> <asp:LinkButton ID="CancleLink" runat="server" CommandName="Cancel" Text="Cancel" /> </td> <td> <asp:Label ID="IDLabel" runat="server" Text='<%#Eval("ID") %>'/> </td> <td > <asp:TextBox ID="CourseNameTextBox" runat="server" Text='<%#Eval("CourseName") %>'></asp:TextBox> </td> </tr> <tr runat="server"> <td> <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%#Eval("Description")%>'></asp:TextBox> <br /> <asp:TextBox ID="StartDateTextBox" runat="server" Text='<%#Eval("StartDate") %>'></asp:TextBox> <br /> <asp:TextBox ID="EndDateTextBox" runat="server" Text='<%#Eval("EndDate") %>'></asp:TextBox> <br /> <asp:DropDownList ID="CourseModeDdl" runat="server"> <asp:ListItem>ClassRoom</asp:ListItem> <asp:ListItem>ELearning</asp:ListItem> <asp:ListItem>Online</asp:ListItem> </asp:DropDownList> <br /> </td> </tr> </EditItemTemplate> <EmptyDataTemplate> <hr /> No Records Found.<hr /> <asp:LinkButton ID="InsertLink" runat="server" Text="Insert Records" CommandName="Insert" /> </EmptyDataTemplate> <InsertItemTemplate> <tr runat="server"> <td > CourseName: <asp:TextBox ID="CourseNameTextBox" runat="server" Text='<%#Eval("CourseName") %>'></asp:TextBox> <br /> Description: <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%#Eval("Description")%>'></asp:TextBox> <br/> StartDate: <asp:TextBox ID="StartDateTextBox" runat="server" Text='<%#Eval("StartDate") %>'></asp:TextBox> <br/> EndDate: <asp:TextBox ID="EndDateTextBox" runat="server" Text='<%#Eval("EndDate") %>'></asp:TextBox> <br /> CourseMode: <asp:DropDownList ID="CourseModeDdl" runat="server" > <asp:ListItem>ClassRoom</asp:ListItem> <asp:ListItem>ELearning</asp:ListItem> <asp:ListItem>Online</asp:ListItem> </asp:DropDownList> </td> </tr> </InsertItemTemplate> </asp:ListView> <asp:DataPager runat="server" ID="DataPager1" PageSize="5" PagedControlID="CourseListView"> <Fields> <asp:NumericPagerField ButtonCount="5" PreviousPageText="<--" NextPageText="-->" /> </Fields> </asp:DataPager> 。还不清楚为什么?

1 个答案:

答案 0 :(得分:8)

好的,刚刚到底:

  1. desc的调用没有任何效果,如果没有它,示例就无法运行
  2. 关键是,当您group_by多个列时,似乎结果会自动按组排序。在上面的示例中,它按cyl排序。要获得整个数据表的预期排序,您必须先ungroup然后arrange

    mtcars %>% group_by( cyl, gear ) %>% 
       summarize( hp_range = max(hp) - min(mpg)) %>% 
       ungroup() %>% 
       arrange( hp_range )