为变量号拆分向量。次进入嵌套向量
Splitting a vector for variable no. of times into nested vectors
我有一个向量,比如说 f1
。
f1 = c(1, 0, 0, 1, 0, 1, 1, 1, 0, 1, -6)
我想根据原始列表中 1 的位置,将此向量拆分为嵌套的列表列表,如下所示。相同的R代码如下:
dfg <- function(f1) {
df<-which(f1 %in% 1)
m<-c()
for(i in 1:df[2]-1) {
m<-c(m,list(f1[i]))
}
m<-c(m,list(f1[df[2]:length(f1)]))
return(m)
}
输出如下:
[[1]]
numeric(0)
[[2]]
[1] 1
[[3]]
[1] 0
[[4]]
[1] 0
[[5]]
[1] 1 0 1 1 1 0 1 -6
到目前为止一切顺利。现在请注意 m[[5]] 的长度 > 1。
我想重复上面的过程,直到输出中每一层嵌套的每个元素的长度为1。
如何确保上面列出的 R 函数针对任意级别的嵌套执行?
编辑:按照评论中的要求,我给出了整个事情所需的最终结果如下。
[[1]] numeric(0)
[[2]] [1] 1
[[3]] [1] 0
[[4]] [1] 0
[[5]]
[[5]][[1]] numeric(0)
[[5]][[2]] [1] 1
[[5]][[3]] [1] 0
[[5]][[4]]
[[5]][[4]][[1]] numeric(0)
[[5]][[4]][[2]] [1] 1
[[5]][[4]][[3]]
[[5]][[4]][[3]][[1]] numeric(0)
[[5]][[4]][[3]][[2]] [1] 1
[[5]][[4]][[3]][[3]]
[[5]][[4]][[3]][[3]][[1]] numeric(0)
[[5]][[4]][[3]][[3]][[2]] [1] 1
[[5]][[4]][[3]][[3]][[3]] [1] 0
[[5]][[4]][[3]][[3]][[4]] [1] 1 -6
(我知道最后一个元素的长度是2,不是1。但这对我来说不是问题。我会在函数dfg本身中解决它。)
虽然对我来说有点神秘为什么你会想要这个嵌套结构,但我能想到的最自然的构造它的方法是使用递归函数:
f1 = c(1, 0, 0, 1, 0, 1, 1, 1, 0, 1, -6)
dfg <- function(f1) {
df <- which(f1 == 1)
if (length(df) <= 1) f1
else c(list(numeric(0)), as.list(f1[1:(df[2]-1)]), list(dfg(f1[df[2]:length(f1)])))
}
dfg(f1)
# [[1]]
# numeric(0)
#
# [[2]]
# [1] 1
#
# [[3]]
# [1] 0
#
# [[4]]
# [1] 0
#
# [[5]]
# [[5]][[1]]
# numeric(0)
#
# [[5]][[2]]
# [1] 1
#
# [[5]][[3]]
# [1] 0
#
# [[5]][[4]]
# [[5]][[4]][[1]]
# numeric(0)
#
# [[5]][[4]][[2]]
# [1] 1
#
# [[5]][[4]][[3]]
# [[5]][[4]][[3]][[1]]
# numeric(0)
#
# [[5]][[4]][[3]][[2]]
# [1] 1
#
# [[5]][[4]][[3]][[3]]
# [[5]][[4]][[3]][[3]][[1]]
# numeric(0)
#
# [[5]][[4]][[3]][[3]][[2]]
# [1] 1
#
# [[5]][[4]][[3]][[3]][[3]]
# [1] 0
#
# [[5]][[4]][[3]][[3]][[4]]
# [1] 1 -6
请注意,我利用 as.list(1:3)
等同于 list(1, 2, 3)
的事实来简化 dfg
中列表的构造。
我有一个向量,比如说 f1
。
f1 = c(1, 0, 0, 1, 0, 1, 1, 1, 0, 1, -6)
我想根据原始列表中 1 的位置,将此向量拆分为嵌套的列表列表,如下所示。相同的R代码如下:
dfg <- function(f1) {
df<-which(f1 %in% 1)
m<-c()
for(i in 1:df[2]-1) {
m<-c(m,list(f1[i]))
}
m<-c(m,list(f1[df[2]:length(f1)]))
return(m)
}
输出如下:
[[1]]
numeric(0)
[[2]]
[1] 1
[[3]]
[1] 0
[[4]]
[1] 0
[[5]]
[1] 1 0 1 1 1 0 1 -6
到目前为止一切顺利。现在请注意 m[[5]] 的长度 > 1。
我想重复上面的过程,直到输出中每一层嵌套的每个元素的长度为1。
如何确保上面列出的 R 函数针对任意级别的嵌套执行?
编辑:按照评论中的要求,我给出了整个事情所需的最终结果如下。
[[1]] numeric(0)
[[2]] [1] 1
[[3]] [1] 0
[[4]] [1] 0
[[5]]
[[5]][[1]] numeric(0)
[[5]][[2]] [1] 1
[[5]][[3]] [1] 0
[[5]][[4]]
[[5]][[4]][[1]] numeric(0)
[[5]][[4]][[2]] [1] 1
[[5]][[4]][[3]]
[[5]][[4]][[3]][[1]] numeric(0)
[[5]][[4]][[3]][[2]] [1] 1
[[5]][[4]][[3]][[3]]
[[5]][[4]][[3]][[3]][[1]] numeric(0)
[[5]][[4]][[3]][[3]][[2]] [1] 1
[[5]][[4]][[3]][[3]][[3]] [1] 0
[[5]][[4]][[3]][[3]][[4]] [1] 1 -6
(我知道最后一个元素的长度是2,不是1。但这对我来说不是问题。我会在函数dfg本身中解决它。)
虽然对我来说有点神秘为什么你会想要这个嵌套结构,但我能想到的最自然的构造它的方法是使用递归函数:
f1 = c(1, 0, 0, 1, 0, 1, 1, 1, 0, 1, -6)
dfg <- function(f1) {
df <- which(f1 == 1)
if (length(df) <= 1) f1
else c(list(numeric(0)), as.list(f1[1:(df[2]-1)]), list(dfg(f1[df[2]:length(f1)])))
}
dfg(f1)
# [[1]]
# numeric(0)
#
# [[2]]
# [1] 1
#
# [[3]]
# [1] 0
#
# [[4]]
# [1] 0
#
# [[5]]
# [[5]][[1]]
# numeric(0)
#
# [[5]][[2]]
# [1] 1
#
# [[5]][[3]]
# [1] 0
#
# [[5]][[4]]
# [[5]][[4]][[1]]
# numeric(0)
#
# [[5]][[4]][[2]]
# [1] 1
#
# [[5]][[4]][[3]]
# [[5]][[4]][[3]][[1]]
# numeric(0)
#
# [[5]][[4]][[3]][[2]]
# [1] 1
#
# [[5]][[4]][[3]][[3]]
# [[5]][[4]][[3]][[3]][[1]]
# numeric(0)
#
# [[5]][[4]][[3]][[3]][[2]]
# [1] 1
#
# [[5]][[4]][[3]][[3]][[3]]
# [1] 0
#
# [[5]][[4]][[3]][[3]][[4]]
# [1] 1 -6
请注意,我利用 as.list(1:3)
等同于 list(1, 2, 3)
的事实来简化 dfg
中列表的构造。