我的表格如下:
# Year Month WaterYear
# 1993 3
# 2000 4
# 2013 10
# 2015 6
# 2000 7
# 2008 12
# 2008 9
# 2012 10
# 2000 11
# 2000 12
我正在尝试通过计算WaterYear等于年份+ 1来更新此表,其中月份范围介于10月到12月之间。
我正在研究R并希望找到最简单的方法来实现它。
答案 0 :(得分:3)
简单的/// <reference path="../../typings/globals/es6-shim/index.d.ts"/>
/// <reference path="../../typings/globals/jasmine/index.d.ts" />
import { inject, TestBed, ComponentFixture } from "@angular/core/testing";
import { RouterTestingModule } from "@angular/router/testing";
import { Headers, HttpModule, BaseRequestOptions, XHRBackend, Response, Http, ResponseOptions } from "@angular/http";
import { MockBackend, MockConnection } from "@angular/http/testing";
import { Component, DebugElement } from "@angular/core";
import { By } from '@angular/platform-browser';
import "rxjs/add/operator/toPromise";
import { Observable } from "rxjs/Rx";
import "rxjs/add/operator/toPromise";
import { AuthenticationLibrary } from "../../app/services/authenticationLibrary.service";
import { ConfigurationService } from "../../app/services/configuration.service";
import { FrontEndLoggingService } from "../../app/services/frontEndLogging.service";
import { JwtService } from "../../app/services/jwt.service";
import { UtilitiesService } from "../../app/services/utilities.service";
describe("Authentication service", () => {
let mockBackend;
let jwtService;
let configSvc;
let http;
let feLogSvc;
let mockHttpResponse;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpModule],
providers: [
{ provide: XHRBackend, useClass: MockBackend },
ConfigurationService,
FrontEndLoggingService,
JwtService,
UtilitiesService
]
});
mockBackend = TestBed.get(XHRBackend);
http = new Http(mockBackend, new BaseRequestOptions);
feLogSvc = TestBed.get(FrontEndLoggingService);
configSvc = TestBed.get(ConfigurationService);
configSvc.config = {
enableFrontEndDebugLogs: true,
baseUrl: "www.someurl.com",
providerEndpoint: "someMachine",
categoriesEndpoint: "someCatEndpoint"
}
jwtService = new JwtService(
TestBed.get(Http),
configSvc,
TestBed.get(UtilitiesService)
)
});
it("Should get token", () => {
let resOp = new ResponseOptions({
body: {
"token": "Bearer ABC123",
"brand": "AMI"
}
})
mockHttpResponse = new Response(resOp)
mockBackend.connections.subscribe(connection => {
connection.mockRespond(mockHttpResponse);
});
jwtService.getToken().subscribe(response => {
expect(response.token).toBe("Bearer ABC123");
});
});
it("Should handle error", () => {
let resOp = new ResponseOptions({
body: "sdvhfujvn"
});
mockHttpResponse = new Response(resOp);
mockBackend.connections.subscribe(connection => {
connection.mockRespond(mockHttpResponse);
});
spyOn(jwtService, 'handleError');
jwtService.getToken().subscribe((res) => {
expect(jwtService.handleError).toHaveBeenCalled();
});
});
});
函数可以解决这个问题。
来自您的数据。
ifelse
结果
# Create data
Year <- c(1993, 2000, 2013, 2015, 2000, 2008, 2008, 2012, 2000, 2000)
Month <- c(3, 4, 10, 6, 7, 12, 9 ,10, 11, 12)
WaterYear <- rep("",length(Year))
dat <- data.frame(Year, Month, WaterYear)
# If month is greater or equal to 10 change it to Year +1,
# otherwise keep it as it is
dat$WaterYear <- ifelse(dat$Month >=10, Year+1, WaterYear)
答案 1 :(得分:0)
我们也可以
i1 <- dat$Month >=10
dat$WaterYear[i1] <- dat$Year[i1] + 1
dat
# Year Month WaterYear
#1 1993 3
#2 2000 4
#3 2013 10 2014
#4 2015 6
#5 2000 7
#6 2008 12 2009
#7 2008 9
#8 2012 10 2013
#9 2000 11 2001
#10 2000 12 2001
或使用data.table
,将'data.frame'转换为'data.table'(setDT(dat)
),在'i'(Month >= 10
)中指定逻辑条件,然后分配(:=
)'年'+ 1''WaterYear'
library(data.table)
setDT(dat)[Month >=10, WaterYear := as.character(Year + 1)]