2017-03-15 55 views
0

我試圖查看是否有更好的方法將所有連續的唯一值放入一個組中。如何根據連續的唯一值將向量拆分爲列表

請注意,該方法必須工作時,x也是字符。

#DATA 
x = c(0, 0, 0, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7) 
x 
#[1] 0 0 0 7 7 7 7 0 0 0 0 0 0 0 7 7 7 7 

#DESIRED OUTPUT 
L = list(c(0, 0, 0), c(7, 7, 7, 7), c(0, 0, 0, 0, 0, 0, 0), c(7, 7, 7, 7)) 
L 
#[[1]] 
#[1] 0 0 0 

#[[2]] 
#[1] 7 7 7 7 

#[[3]] 
#[1] 0 0 0 0 0 0 0 

#[[4]] 
#[1] 7 7 7 7 

#CURRENT APPROACH 
split_vector = 0 
for (i in 2:length(x)){ 
    split_vector[i] = ifelse(x[i] != x[i-1], max(split_vector) + 1, split_vector[i-1]) 
} 
split(x, split_vector) 
#$`0` 
#[1] 0 0 0 

#$`1` 
#[1] 7 7 7 7 

#$`2` 
#[1] 0 0 0 0 0 0 0 

#$`3` 
#[1] 7 7 7 7 

回答

3
tapply(x, cumsum(c(TRUE, diff(x) != 0)), identity) 

$`1` 
[1] 0 0 0 

$`2` 
[1] 7 7 7 7 

$`3` 
[1] 0 0 0 0 0 0 0 

$`4` 
[1] 7 7 7 7 

# Character example 
x <- rep(c("a", "b", "c", "a"), c(4, 3, 2, 4)) 
x 

[1] "a" "a" "a" "a" "b" "b" "b" "c" "c" "a" "a" "a" "a" 

# Character version 
tapply(x, cumsum(c(TRUE, x[-1] != x[-length(x)])), identity) 

$`1` 
[1] "a" "a" "a" "a" 

$`2` 
[1] "b" "b" "b" 

$`3` 
[1] "c" "c" 

$`4` 
[1] "a" "a" "a" "a" 
+1

酷。對於字符,'tapply(x,cumsum(c(TRUE,diff(as.numeric(as.factor(x))!!= 0)),I)'作品 –

+1

請參閱更新的。 – JasonWang

5

以下是一些備選:

1)使用rlerep,以形成在該分組載體和分裂。沒有包被使用。

split(x, with(rle(x), rep(seq_along(values), lengths))) 

,並提供:

$`1` 
[1] 0 0 0 

$`2` 
[1] 7 7 7 7 

$`3` 
[1] 0 0 0 0 0 0 0 

$`4` 
[1] 7 7 7 7 

2)從data.table包使用rleid是,更容易:

library(data.table) 
split(x, rleid(x)) 
相關問題