一种方法是利用unlist()
自动创建的名称层次结构。这也将使F=c("G", "H")
和F=list("G", "H")
以相同的方式处理。
下面的示例不允许节点名称中包含数字,并且节点名称必须是唯一的。这可以通过使用rapply()
方法进行改进。
定义替代方括号
squarebracketsAlt <- function(inlist){
storeList <- unlist(inlist)
uniqueNames <- unique(unlist(strsplit(gsub("[0-9]", "", names(storeList)), "\\.")))
vecNames <- names(storeList)
storeVec <- paste0("[", storeList, "]")
names(storeVec) <- vecNames
for(i in uniqueNames){
whereBrack <- grep(paste0("\\.",i, "\\."),
paste0(".", gsub("[0-9]", "", names(storeVec)), "."))
storeVec <- append(storeVec, paste0("[", i), after=(whereBrack[1]-1))
storeVec <- append(storeVec, paste0("]") , after=(whereBrack[length(whereBrack)]+1))
}
cat(paste(storeVec, collapse=""))
}
在您的嵌套列表中尝试它:
nestedlist <- list("A"=list("B"=45:50, "C"=LETTERS[21:26],
"D"=list("E"=7:10, "F"=list("G","H"))))
squarebracketsAlt(nestedlist)
输出:
[A[B[45][46][47][48][49][50]][C[U][V][W][X][Y][Z]][D[E[7][8][9][10]][F[G][H]]]]
![enter image description here](https://istack.dev59.com/u9bZa.webp)
示例更大的层次结构:
nestedlist1 <- list("Ad fe"=list("B"=45:50, "C"=list("U"=letters[1:10],LETTERS[22:26]),
"D"=list("E"=7:10, "F"=list("G"=list("ZZ foo"=list("AA bar"=c(1:10),2,3,4,5)),"H", "C"))))
squarebracketsAlt(nestedlist1)
输出:
[Ad fe[B[45][46][47][48][49][50]][C[U[a][b][c][d][e][f][g][h][i][j]][V][W][X][Y][Z]][D[E[7][8][9][10]][F[G[ZZ foo[AA bar[1][2][3][4][5][6][7][8][9][10]][2][3][4][5]]][H][C]]]]
![enter image description here](https://istack.dev59.com/x69VC.webp)
现实生活中的例子:
nestedlist2 <- list("Main Area"=
list("Fishing vessel"=c("trawler", "line", "skipper"), "Oil tanker"=c("Large", "Small", "Medium size"=
list("Barents Sea", "Norwegian Sea", "Kara Sea", "Greenland"))))
squarebracketsAlt(nestedlist2)
输出:
[Main Area[Fishing vessel[trawler][line][skipper]][Oil tanker[Large][Small][Medium size[Barents Sea][Norwegian Sea][Kara Sea][Greenland]]]]
![enter image description here](https://istack.dev59.com/rWNQI.webp)
带有前导数字的示例:
squarebracketsAltNum <- function(inlist){
storeList <- unlist(inlist)
uniqueNames <- unique(paste(gsub("[A-z].*", "", unlist(strsplit(names(storeList), "\\."))),
unlist(strsplit(gsub("[0-9]", "", names(storeList)), "\\.")), sep=""))
vecNames <- names(storeList)
storeVec <- paste0("[", storeList, "]")
names(storeVec) <- vecNames
k <- 1
for(i in uniqueNames){
cat(i, "\n")
whereBrack <- grep(paste0("\\.",i),
paste0(".", names(storeVec)))
namePaster <- unique(paste(unlist(strsplit(gsub("[0-9]", "", names(storeList)), "\\.")),
gsub("[A-z].*", "", unlist(strsplit(names(storeList), "\\."))), sep=""))[k]
storeVec <- append(storeVec, paste0("[", namePaster), after=(whereBrack[1]-1))
storeVec <- append(storeVec, paste0("]") , after=(whereBrack[length(whereBrack)]+1))
k <- k+1
}
cat(paste(storeVec, collapse=""))
}
数字和单词/句子之间不应有空格。使用正则表达式进行调整以解决此问题:
nestedlist <- list("100A"=list("4B"=45:50, "3C"=LETTERS[21:26],
"D"=list("E"=7:10, "78F"=c("G","H"))))
squarebracketsAlt(nestedlist)
输出:
[A100[B4[45][46][47][48][49][50]][C3[U][V][W][X][Y][Z]][D[E[7][8][9][10]][F78[G][H]]]]
nestedlist
中的“F”应该被定义为F=c("G", "H")
而不是F=list("G", "H")
吗? - henrik_ibsen