假设我有start=1
、end=12
和区间start:end
。
我想把它分成x=2
的仓,这样我就可以得到的数据帧
start.index end.index
1 1 2
2 3 4
3 5 6
4 7 8
5 9 10
6 11 12
在这种情况下,结果是6个仓。start
、end
和x
始终是整数
有什么功能可以做到这一点吗?很明显,当start%%x!=0
时,一个箱子可能比其他箱子大或小,但我不介意。
有什么帮助吗?
下面是这样一个函数的简单示例:
foo <- function(start, end, x = 2) {
SEQ <- seq(start, end, by = x)
END <- SEQ + (x - 1)
take <- END > end
END[take] <- end
data.frame(start.index = SEQ, end.index = END)
}
R> foo(1, 12, 2)
start.index end.index
1 1 2
2 3 4
3 5 6
4 7 8
5 9 10
6 11 12
R> foo(1, 12, 3)
start.index end.index
1 1 3
2 4 6
3 7 9
4 10 12
R> foo(1, 12, 4)
start.index end.index
1 1 4
2 5 8
3 9 12
通过奇数次的观测,我们得到了最后一个不同的仓宽度:
R> foo(1, 11)
start.index end.index
1 1 2
2 3 4
3 5 6
4 7 8
5 9 10
6 11 11