Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何根据连续的唯一值将向量拆分为列表_R - Fatal编程技术网

R 如何根据连续的唯一值将向量拆分为列表

R 如何根据连续的唯一值将向量拆分为列表,r,R,我想看看是否有更好的方法将向量拆分为列表,这样所有连续的唯一值都放在一个组中 注意,当x也是字符时,该方法必须工作 以下是一些备选方案: 1使用rle和rep形成分组向量,并在该向量上拆分。没有使用任何软件包 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.

我想看看是否有更好的方法将向量拆分为列表,这样所有连续的唯一值都放在一个组中

注意,当x也是字符时,该方法必须工作


以下是一些备选方案:

1使用rle和rep形成分组向量,并在该向量上拆分。没有使用任何软件包

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))

以下是一些备选方案:

1使用rle和rep形成分组向量,并在该向量上拆分。没有使用任何软件包

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))
凉的对于字符,tapplyx、cumsumcTRUE、diffas.numericas.factorx!=0,我很酷。对于字符,tapplyx、cumsumcTRUE、diffas.numericas.factorx!=0,我工作