我试图获得连续行之间的差异,例如:
dt_in <- data.table(
Game = c(1,1,2,2,3,3,4,4),
ID = c(1,2,3,4,5,6,7,8),
weight = c(150,120,151,160,190,170,170,170)
)
我想计算每个ID,他们的权重比Game
中的其他ID多多少少。到目前为止我所做的是:
dt_in[, d1 := diff(weight), by = list(Game)]
给出:
> dt_in
Game ID weight d1
1: 1 1 150 -30
2: 1 2 120 -30
3: 2 3 151 9
4: 2 4 160 9
5: 3 5 190 -20
6: 3 6 170 -20
7: 4 7 170 0
8: 4 8 170 0
但我想要的是:
> dt_in
Game ID weight d1 want
1: 1 1 150 -30 30
2: 1 2 120 -30 -30
3: 2 3 151 9 -9
4: 2 4 160 9 9
5: 3 5 190 -20 20
6: 3 6 170 -20 -20
7: 4 7 170 0 0
8: 4 8 170 0 0
答案 0 :(得分:7)
我们可以获得import Foundation
import UIKit
class OrderProduct: NSObject, NSCoding {
var uuid: String = NSUUID().UUIDString
var productcode:String = ""
var detail:String = ""
var quantity:String = ""
var barcode:String = ""
var inOrderList = false
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(uuid, forKey: "uuid")
coder.encodeObject(productcode, forKey: "productcode")
coder.encodeObject(detail, forKey: "detail")
coder.encodeObject(quantity, forKey: "quantity")
coder.encodeObject(barcode, forKey: "barcode")
coder.encodeBool(inOrderList, forKey: "inOrderList")
}
required init?(coder decoder: NSCoder) {
super.init()
if let archivedUuid = decoder.decodeObjectForKey("uuid") as? String {
uuid = archivedUuid
}
if let archivedProductcode = decoder.decodeObjectForKey("productcode") as? String {
productcode = archivedProductcode
}
if let archivedDetail = decoder.decodeObjectForKey("detail") as? String {
detail = archivedDetail
}
if let archivedQuantity = decoder.decodeObjectForKey("quantity") as? String {
quantity = archivedQuantity
}
if let archivedBarcode = decoder.decodeObjectForKey("barcode") as? String {
barcode = archivedBarcode
}
inOrderList = decoder.decodeBoolForKey("inOrderList")
}
init(productcode: String, detail: String, quantity: String, barcode: String) {
super.init()
self.productcode = productcode
self.detail = detail
self.quantity = quantity
self.barcode = barcode
}
}
的权重&#39;按&#39; Game&#39;分组,乘以-1并连接两个值。
import Foundation
import UIKit
//MARK: - Class
class TestOrderViewController: UITableViewController{
required init?(coder decoder: NSCoder) {
//print("init OrderViewController")
super.init(coder: decoder)
loadOrderProducts()
}
deinit {
}
//MARK: - Product Vars
var passedProductcode = String() //String is recieved here!
var passedDetail = String() //String is recieved here!
var passedQuantity = String() //String is recieved here!
var passedBarcode = String() //String is recieved here!
var orderProducts = [OrderProduct]() //My Object model...
//MARK: - ViewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
title = "Order List"
orderProducts.productcode = passedProductcode //Trying to pass them but
orderProducts.detail = passedDetail //errors with "Value of type
orderProducts.quantity = passedQuantity //'[OrderProduct]' has no
orderProducts.barcode = passedBarcode //member 'productcode'?
tableView.estimatedRowHeight = 80.0
tableView.rowHeight = UITableViewAutomaticDimension
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return orderProducts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("OrderCell", forIndexPath: indexPath) as! OrderTableViewCell
let orderProduct = orderProducts[indexPath.row]
cell.productcodeLabel.text = orderProduct.productcode
cell.detailLabel.text = orderProduct.detail
cell.quantityLabel.text = "X \(orderProduct.quantity)"
cell.barcodeLabel.text = orderProduct.barcode
return cell
}
private func loadOrderProducts() {
if let filePath = pathForOrderProducts() where NSFileManager.defaultManager().fileExistsAtPath(filePath) {
if let archivedOrderProducts = NSKeyedUnarchiver.unarchiveObjectWithFile(filePath) as? [OrderProduct] {
orderProducts = archivedOrderProducts
}
}
}
private func pathForOrderProducts() -> String? {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
if let documents = paths.first, let documentsURL = NSURL(string: documents) {
return documentsURL.URLByAppendingPathComponent("orderProducts.plist").path
}
return nil
}
private func saveOrderProducts() {
if let filePath = pathForOrderProducts() {
NSKeyedArchiver.archiveRootObject(orderProducts, toFile: filePath)
}
}
}
@Frank的紧凑选项
diff