0

所以,我有一个长度为 114 的向量:

p2<-c(28.00, 28.00, 28.00, 28.00, 28.00, 28.00, 28.37, 28.37, 28.37, 28.37, 28.37,
     28.37, 28.37, 28.37, 28.37, 28.37, 28.37, 28.37, 28.37,28.37, 28.37, 28.45,
     28.45, 28.45 ,28.45, 31.37, 31.37 ,31.37, 31.37, 31.37, 31.37, 31.37, 31.37,
     31.37, 31.37, 31.37, 31.37 ,31.37, 36.37, 36.37, 36.37 ,36.37, 38.00, 38.00,
     38.00, 38.00, 38.00, 38.00, 38.00, 38.00, 38.00, 38.00, 38.00, 38.00, 38.00,
     38.00 ,38.00, 38.00, 38.00 ,38.00, 38.00, 38.00, 38.00,38.00, 38.00, 38.00,
     38.00, 38.00, 38.00, 38.00, 38.37, 38.37, 38.37, 38.37, 38.37, 38.37, 38.37,
     38.37, 38.37, 38.37, 38.37, 38.37, 41.37, 41.37, 41.37, 41.37, 41.37, 41.37,
     41.37, 41.37, 41.37, 41.37, 41.37, 41.37, 41.37, 41.37, 41.37, 41.37, 41.37,
     41.37, 41.37, 41.37, 41.37, 41.37, 41.37, 41.3,7, 41.37, 41.37, 41.37, 41.37,
     41.37, 41.37, 41.37, 41.3)

我想把它削减成两条记录。我尝试使用 cut,但输出由具有相同长度的间隔定义:(28,34.7] (34.7,41.4]

我怎么做?

4

3 回答 3

2

目前尚不清楚您的预期输出,但我认为您正在寻找:

split(p2,cut(p2,2))
于 2013-11-08T11:14:43.233 回答
1

对于您的特定情况,请尝试

> set.seed(1)
> p2 <- runif(114, 28, 42)  # random data
> 
> ind <- 1:(length(p2)/2) # index for cutting the vector
> p2.1 <- p2[ind]         # first subvector
> p2.2 <- p2[-ind]        # second subvector
> 
> length(p2.1)            # length of first subvector
[1] 57
> length(p2.2)            # length of second subvector
[1] 57

或者可能是这样的?

> vec <- setNames(p2, cut(p2,2, labels=c("V1", "V2")))
> is.vector(vec)  # one vector 
[1] TRUE
于 2013-11-08T11:06:23.583 回答
1

这会给你想要的吗?

library(Hmisc)
cut2(p2, g = 2)

From ?cut2: ...如果cuts没有给出,将 x 分成分位数组(g给出)

于 2013-11-08T11:20:41.580 回答