简体   繁体   中英

Split vector into group by sequence

I have a vector of years

a = c(0.2,1.4,1.8,4.2,6.7,6.8,7.4)

I want to cut this vector into a list such that all a[i] in one year is an element of the list.

eg a will get cut into

$`1`
[1]  0.2
$`2`
[1]  1.4,1.8
$`3`
[1] 4.2

and so on .....

You can use floor() function to round down and use that as your splitting rule, ie

split(a, floor(a))

which gives,

$`0`
[1] 0.2

$`1`
[1] 1.4 1.8

$`4`
[1] 4.2

$`6`
[1] 6.7 6.8

$`7`
[1] 7.4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM