在R中循环以创建计算的delta列

时间:2016-10-04 15:18:50

标签: r function loops

在一段时间后,我有一个带有基线变量和相同变量的数据框。 我试图循环以计算delta列。

import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';
import { Headers, RequestOptions, Http, Response } from '@angular/http';
import { SystemSetup } from '../system-setup/system-setup';

@Component({
  selector: 'app-setup-form',
  templateUrl: 'setup-form.component.html',
  styleUrls: ['setup-form.component.css']
})
export class SetupFormComponent implements OnInit {
    @Input()    selectedItem: SystemSetup; // The object to be edited
    @Output()   finishedEditing = new EventEmitter<number>(); // When the POST is done send to the parent component the new id

    // Inject the Http service into our component
    constructor(private _http: Http) { }

    // User is finished editing POST the object to the server to be saved
    saveEdits(): void {
        let body = JSON.stringify( this.selectedItem );
        let headers = new Headers({ 'Content-Type': 'application/json' });
        let options = new RequestOptions({ headers: headers });

        return this._http.post('http://localhost:8080/**********', body, options)
               .map(this.extractData)
               .do(
                  data => {
                     this.finishedEditing.emit(data.system_setup_id); // Send the parent component the id if the selectedItem
               })
               .toPromise()
               .catch(this.handleError);
    }


    /**
     * Gets the data out of the package from the AJAX call.
     * @param  {Response} res - AJAX response
     * @returns SystemSetup - A json of the returned data
     */
    extractData(res: Response): SystemSetup {
        let body = res.json();
        if (body === 'failed') {
          body = {};
        }
        return body || {};
    }

    /**
     * Handles the AJAX error if the call failed or exited with exception. Print out the error message.
     * @param  {any} error - An error json object with data about the error on it
     * @returns Promise - A promise with the error message in it
     */
    private handleError(error: any): Promise<void> {
        // In a real world app, we might use a remote logging infrastructure
        // We'd also dig deeper into the error to get a better message
        let errMsg = (error.message) ? error.message :
          error.status ? `${error.status} - ${error.statusText}` : 'Server error';
        console.error(errMsg); // log to console instead
        return Promise.reject(errMsg);
    }

}

不幸的是,它仍然无法运作;加上我无法为新列命名。 提前致谢, 甲

1 个答案:

答案 0 :(得分:1)

如果没有关于列的更多信息以及您在问题中使用的逻辑的目标,我不确定您要做什么,但这是如何计算R中数据框内2列之间的增量:

df = data.frame(v1 = c(1,2,3,4,5),v2 = c(5,4,3,2,1))

v3 = df$v1-df$v2

#-4 -2  0  2  4