我正在使用大型data.table。请考虑以下示例:
#1st row is
A <- c(8,8,8,8,8,8,8,8,8,8)
#2nd row is
B <- c(9,9,7,6,5,6,7,7,8,8)
我需要知道B何时开始低于A,何时高于或等于A.
我想要的是这样的表:
begin end
3 9
答案 0 :(得分:6)
以下是使用sign
,
which(c(FALSE, diff(sign(A - B)) != 0))
#[1] 3 9
答案 1 :(得分:2)
以下是var logEnvironment = "TEST"; // TEST for Testing environment, PRODUCTION for production
// Get Ids for the Google Form and Response Spreadsheet
if (logEnvironment == "TEST") {
var premediaFormId = <MY TEST FORM ID>
var responseWorkbookId = *******;
} else {
var premediaFormId = <MY PRODUCTION FORM ID>;
var responseWorkbookId = *******;
}
// Google Form
var premediaForm = FormApp.openById(premediaFormId);
var premediaFormItems = premediaForm.getItems();
rle()
使用A <- c(8,8,8,8,8,8,8,8,8,8)
B <- c(9,9,7,6,5,6,7,7,8,8)
l <- rle(A>B)$lengths
l[1] + 1 ## begin
l[1] + l[2] + 1 ## end
即可:
data.table
或
library("data.table")
w <- which(rleid(A>B)==2)
min(w) ## begin
max(w) + 1 ## end
或
library("data.table")
w <- which(rleid(A>B)==2)
w[1] ## begin
tail(w,1) + 1 ## end
答案 2 :(得分:1)
你也可以试试这个:
begin = which(B<A)[1]
end = which(B>=A)[which(B>=A) > begin][1]