如何获得向量的所有连续子集?

时间:2021-08-04 14:56:31

If I want to get all subsets of a vector I can e.g. use the sets package:

如果我想要得到一个向量的所有子集,我可以使用集合包:

library(sets)
v <- c("test1", "test2", "test3", "test4")
set_power(v)
## {{}, {"test1"}, {"test2"}, {"test3"}, {"test4"}, {"test1",
##  "test2"}, {"test1", "test3"}, {"test1", "test4"}, {"test2",
##  "test3"}, {"test2", "test4"}, {"test3", "test4"}, {"test1",
##  "test2", "test3"}, {"test1", "test2", "test4"}, {"test1",
##  "test3", "test4"}, {"test2", "test3", "test4"}, {"test1",
##  "test2", "test3", "test4"}}

My question
How do I get only all subsets, where all the elements are consecutive, so in the above case without {"test1", "test3"}, {"test1", "test4"}, {"test2", "test4"}, {"test1", "test2", "test4"}, {"test1", "test3", "test4"}

我的问题是,如何只获得所有子集,其中所有元素都是连续的,因此在上面的例子中,没有{"test1", "test3"}, {"test1"}, {"test2"}, {" test4"}, {"test1", "test2", "test4"},

2 个解决方案

#1


15  

Try rollapply like this:

尝试rollapply是这样的:

library(zoo)
v <- c("test1", "test2", "test3", "test4")

L <- lapply(seq_along(v), rollapply, data = v, c)
L[[1]] <- matrix(L[[1]])

giving length(v) components, one for each subset size:

给出长度(v)分量,每一个子集的大小:

[[1]]
     [,1]   
[1,] "test1"
[2,] "test2"
[3,] "test3"
[4,] "test4"

[[2]]
     [,1]    [,2]   
[1,] "test1" "test2"
[2,] "test2" "test3"
[3,] "test3" "test4"

[[3]]
     [,1]    [,2]    [,3]   
[1,] "test1" "test2" "test3"
[2,] "test2" "test3" "test4"

[[4]]
     [,1]    [,2]    [,3]    [,4]   
[1,] "test1" "test2" "test3" "test4"

or as a flat list with one component per subset:

或作为一个单位列表,每个子集包含一个组件:

flat <- do.call("c", lapply(L, function(x) split(x, 1:nrow(x))))

giving:

给:

> str(flat)
List of 10
 $ 1: chr "test1"
 $ 2: chr "test2"
 $ 3: chr "test3"
 $ 4: chr "test4"
 $ 1: chr [1:2] "test1" "test2"
 $ 2: chr [1:2] "test2" "test3"
 $ 3: chr [1:2] "test3" "test4"
 $ 1: chr [1:3] "test1" "test2" "test3"
 $ 2: chr [1:3] "test2" "test3" "test4"
 $ 1: chr [1:4] "test1" "test2" "test3" "test4"

#2


9  

A base R option would be embed

一个基本的R选项将被嵌入

lapply(seq_along(v), function(i) embed(v, i)[, i:1, drop = FALSE])
#[[1]]
#     [,1]   
#[1,] "test1"
#[2,] "test2"
#[3,] "test3"
#[4,] "test4"

#[[2]]
#     [,1]    [,2]   
#[1,] "test1" "test2"
#[2,] "test2" "test3"
#[3,] "test3" "test4"

#[[3]]
#     [,1]    [,2]    [,3]   
#[1,] "test1" "test2" "test3"
#[2,] "test2" "test3" "test4"

#[[4]]
#     [,1]    [,2]    [,3]    [,4]   
#[1,] "test1" "test2" "test3" "test4"

#1


15  

Try rollapply like this:

尝试rollapply是这样的:

library(zoo)
v <- c("test1", "test2", "test3", "test4")

L <- lapply(seq_along(v), rollapply, data = v, c)
L[[1]] <- matrix(L[[1]])

giving length(v) components, one for each subset size:

给出长度(v)分量,每一个子集的大小:

[[1]]
     [,1]   
[1,] "test1"
[2,] "test2"
[3,] "test3"
[4,] "test4"

[[2]]
     [,1]    [,2]   
[1,] "test1" "test2"
[2,] "test2" "test3"
[3,] "test3" "test4"

[[3]]
     [,1]    [,2]    [,3]   
[1,] "test1" "test2" "test3"
[2,] "test2" "test3" "test4"

[[4]]
     [,1]    [,2]    [,3]    [,4]   
[1,] "test1" "test2" "test3" "test4"

or as a flat list with one component per subset:

或作为一个单位列表,每个子集包含一个组件:

flat <- do.call("c", lapply(L, function(x) split(x, 1:nrow(x))))

giving:

给:

> str(flat)
List of 10
 $ 1: chr "test1"
 $ 2: chr "test2"
 $ 3: chr "test3"
 $ 4: chr "test4"
 $ 1: chr [1:2] "test1" "test2"
 $ 2: chr [1:2] "test2" "test3"
 $ 3: chr [1:2] "test3" "test4"
 $ 1: chr [1:3] "test1" "test2" "test3"
 $ 2: chr [1:3] "test2" "test3" "test4"
 $ 1: chr [1:4] "test1" "test2" "test3" "test4"

#2


9  

A base R option would be embed

一个基本的R选项将被嵌入

lapply(seq_along(v), function(i) embed(v, i)[, i:1, drop = FALSE])
#[[1]]
#     [,1]   
#[1,] "test1"
#[2,] "test2"
#[3,] "test3"
#[4,] "test4"

#[[2]]
#     [,1]    [,2]   
#[1,] "test1" "test2"
#[2,] "test2" "test3"
#[3,] "test3" "test4"

#[[3]]
#     [,1]    [,2]    [,3]   
#[1,] "test1" "test2" "test3"
#[2,] "test2" "test3" "test4"

#[[4]]
#     [,1]    [,2]    [,3]    [,4]   
#[1,] "test1" "test2" "test3" "test4"