根据间隔对矢量进行分类

时间:2015-09-30 11:24:16

标签: r vector

我有一个离散变量P代表propabilites

<Stilling>Tekstilselger</Stilling>
<Arbeidssted>Bergen</Arbeidssted>
<Arbeidsgiver>XXL</Arbeidsgiver>
<Datoregistrert>23.09.2015</Datoregistrert>
<Frist>06.10.2015</Frist>

<Stilling>Sushi Chef</Stilling>
<Arbeidssted>Bergen</Arbeidssted>
<Arbeidsgiver>Zupperia Matbørsen</Arbeidsgiver>
<Datoregistrert>23.09.2015</Datoregistrert>
<Frist>30.10.2015</Frist>

<Stilling>Psykolog</Stilling>
<Arbeidssted>Fjell</Arbeidssted>
<Arbeidsgiver>Haukeland Universitetsykehus</Arbeidsgiver>
<Datoregistrert>22.09.2015</Datoregistrert>
<Frist>03.10.2015</Frist>

<Stilling>Butikkansvarlig</Stilling>
<Arbeidssted>Bergen</Arbeidssted>
<Arbeidsgiver>Spraytanhuset</Arbeidsgiver>
<Datoregistrert>18.09.2015</Datoregistrert>
<Frist>04.10.2015</Frist>

我想重新排列此变量并将其更改为序数

类别:

 p <- c(0.5,  0.4,  0.5,  0.1,  0.8,  0.9 )

1 个答案:

答案 0 :(得分:4)

您可以使用cut功能,如下所示

p <- c(0.5,  0.4,  0.5,  0.1,  0.8,  0.9 )

cut(p, breaks = seq(0, 1, 0.2), include.lowest = TRUE)
#[1] (0.4,0.6] (0.2,0.4] (0.4,0.6] [0,0.2]   (0.6,0.8] (0.8,1]  
#Levels: [0,0.2] (0.2,0.4] (0.4,0.6] (0.6,0.8] (0.8,1]

如果您想更改标签,请尝试labels参数,例如

cut(p, breaks = seq(0, 1, 0.2), labels = paste0("0", 1:5), include.lowest = TRUE)
#[1] 03 02 03 01 04 05
#Levels: 01 02 03 04 05