在data.table中使用多行表达式时的最佳实践

时间:2015-09-24 09:52:25

标签: r coding-style data.table

假设我们想在data.table的{}内使用j对data.table执行“错综复杂的”操作系列,la:

data(mtcars)
carName <- row.names(mtcars)
mtcars <- data.table(mtcars)
mtcars[, car_name := carName]

mtcars[, {
  num_cars <- length(unique(car_name))
  avg_disp <- mean(disp)
  avg_hp <- mean(hp)
  eff <- mpg/wt
  data.table(num_cars, avg_disp, avg_hp,
             avg_eff = mean(eff), median_eff = median(eff))
}, keyby = cyl]

我知道这可以通过几个步骤执行:

mtcars[, `:=`(num_cars = length(unique(car_name)),
              avg_disp = mean(disp),
              avg_hp = mean(hp),
              eff = mpg/wt),
       keyby = cyl]

mtcars[, `:=`(avg_eff = mean(eff), median_eff = median(eff)), keyby = cyl]
mtcars[, .SD[1,list(num_cars, avg_disp, avg_hp, avg_eff, median_eff)], 
       keyby = cyl]

但我有兴趣知道最常用的方式(阅读:最佳实践)是如何执行此类事情的。

在数据表格的{}中,j内的<!DOCTYPE html> <html> <head> <title>Angular JS table sort and filter example </title> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css"> <script src="http://code.angularjs.org/1.2.16/angular.js"></script> <script src="http://code.angularjs.org/1.2.16/angular-resource.js"></script> <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.11.0.js"></script> <script> var app = angular.module('MyForm', ['ui.bootstrap', 'ngResource']); app.controller('myCtrl', function ($scope) { $scope.predicate = 'name'; $scope.reverse = true; $scope.currentPage = 1; $scope.order = function (predicate) { $scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false; $scope.predicate = predicate; }; $scope.students = [ { name: 'Kevin', age: 25, gender: 'boy' }, { name: 'John', age: 30, gender: 'girl' }, { name: 'Laura', age: 28, gender: 'girl' }, { name: 'Joy', age: 15, gender: 'girl' }, { name: 'Mary', age: 28, gender: 'girl' }, { name: 'Peter', age: 95, gender: 'boy' }, { name: 'Bob', age: 50, gender: 'boy' }, { name: 'Erika', age: 27, gender: 'girl' }, { name: 'Patrick', age: 40, gender: 'boy' }, { name: 'Tery', age: 60, gender: 'girl' } ]; $scope.totalItems = $scope.students.length; $scope.numPerPage = 5; $scope.paginate = function (value) { var begin, end, index; begin = ($scope.currentPage - 1) * $scope.numPerPage; end = begin + $scope.numPerPage; index = $scope.students.indexOf(value); return (begin <= index && index < end); }; }); </script> <style> .odd { background-color: antiquewhite; color: #008b8b; } td th { height: 30px; min-width: 100px; } thead { background-color: darkgray; color: white; height: 30px; } </style> </head> <body ng-app="MyForm"> <div ng-controller="myCtrl"> <h3>List students</h3> <div class="container-fluid"> <pre>Click header link to sort, input into filter text to filter</pre> <hr /> <table class="table table-striped"> <thead> <tr> <th>Edit</th> <th> <a href="" ng-click="order('name')">Name</a> </th> <th><a href="" ng-click="order('age')"> Age</a> </th> <th><a href="" ng-click="order('gender')">Gender</a> </th> </tr> </thead> <tbody> <tr> <td>Filter =>></td> <td> <input type="text" ng-model="search.name" /></td> <td> <input type="text" ng-model="search.age" /> </td> <td><input type="text" ng-model="search.gender" /> </td> </tr> <tr ng-repeat="user in students | orderBy:predicate:reverse | filter:paginate| filter:search" ng-class-odd="'odd'"> <td> <button class="btn"> Edit </button> </td> <td>{{ user.name}}</td> <td>{{ user.age}}</td> <td>{{ user.gender}}</td> </tr> </tbody> </table> <pagination total-items="totalItems" ng-model="currentPage" max-size="5" boundary-links="true" items-per-page="numPerPage" class="pagination-sm"> </pagination> </div> </div> </body> </html> 内工作是不是很不受欢迎?

0 个答案:

没有答案