我想将一组数据分成四个相等大小的间隔,例如:
x=c(0:10)
G1: [0,2.5)
G2: [2.5,5)
G3: [5,7.5)
G4: [7.5,10]
无论如何,我可以将数据分成相等长度的间隔(在本例中为4),并获得包括最小值和最大值的下面列表:
0 2.5 5 7.5 10
谢谢!
答案 0 :(得分:1)
您正在寻找cut
cut(1:10, c(1, seq(2.5, 10, by=2.5)), include.lowest = T)
# [1] [1,2.5] [1,2.5] (2.5,5] (2.5,5] (2.5,5] (5,7.5] (5,7.5] (7.5,10]
# [9] (7.5,10] (7.5,10]
# Levels: [1,2.5] (2.5,5] (5,7.5] (7.5,10]
如果您只想要均匀间隔的休息时间,请使用seq
x <- 0:10
seq(min(x), max(x), len=5)
# [1] 0.0 2.5 5.0 7.5 10.0
答案 1 :(得分:0)
data.frame(x, breaks=cut(x,breaks = 5, labels=c("1","2.5","5","7.5","10")) )
有效吗? (当然,诀窍在于削减功能)