想通过cutoff 将df框架拆分为嵌套df.listing列表index_cutoff:
数据:
df <- data.frame(m=c("A","T","W","Z","B","A","A","W","T","K","G","B","T","B"))
index_cutoff <- c("A","B")
Run Code Online (Sandbox Code Playgroud)
尝试代码:
df.listing <- split(df, df$m %in% keyword_cutoff) #failed, not working
Run Code Online (Sandbox Code Playgroud)
电流输出:
$`FALSE`
m
2 T
3 W
4 Z
8 W
9 T
10 K
11 G
13 T
$`TRUE`
m
1 A
5 B
6 A
7 A
12 B
14 B
Run Code Online (Sandbox Code Playgroud)
期望的输出阶段1:
df.listing[[1]]
A
T
W
Z
df.listing[[2]]
B
df.listing[[3]]
A
df.listing[[4]]
A
W
T
K
G
df.listing[[5]]
B
T
df.listing[[6]]
B
Run Code Online (Sandbox Code Playgroud)
期望的输出最终:
df.listing[[1]]
A
T
W
Z
df.listing[[2]]
B
df.listing[[3]]
A #since at stage 1 they are the same cutoff, hence self merge into next list
A
W
T
K
G
df.listing[[4]]
B #since at stage 1 they begin the same with "B" cutoff
T
B
Run Code Online (Sandbox Code Playgroud)
感谢您并且不能通过R数据集提供可重现的示例.
我们需要将逻辑索引的累积和作为拆分组
split(df, cumsum(df$m %in% index_cutoff))
Run Code Online (Sandbox Code Playgroud)
在OP的代码中,只有两个组,即TRUE和FALSE df$m %in% index_cutoff.通过这样做cumsum,它通过在每个TRUE值加1来改变