使用默认 R 包将嵌套列表定义为向量
Defining nested list into a vector with default R packages
myList <- rep(0, 5)
myList <- list('Aa'= c("1","12"), 'Ba'= c("123","321"), 'Ca'= c("444"))
for (x in names(myList)) {
for (j in myList[x]) {
sample <- c(unlist(myList[j]))
} }
sample
在嵌套列表中,想要将此列表转换为具有
的向量
"for"
循环
我不知道如何在不定义函数的情况下提取值。
sample <- c(unlist(myList[x])
执行“unlist”只需使用其他代码即可完成工作
> sample
NULL
但我想要全部,而这个不是这样的。有基础版吗?无需快速或简单。只有基本的 R 分发包,
alternative of "unlist"
如果可能的话。
我需要类似的东西
Aa1 Aa2 Ba1 Ba2 Ca1
"1" "12" "123" "444"
我想我不需要
myList <- rep(0, 5)
我可能不太理解你的问题,但不是
sample <- unlist(myList)
# Aa1 Aa2 Ba1 Ba2 Ca
# "1" "12" "123" "321" "444"
或
sample <- structure(unlist(myList))
# Aa1 Aa2 Ba1 Ba2 Ca
# "1" "12" "123" "321" "444"
实现你想要的?
不太确定为什么要避免 unlist
,但这里有一个替代方案 flatten_chr
。
len <- lengths(myList)
setNames(purrr::flatten_chr(myList),paste0(rep(names(myList),len),sequence(len)))
# Aa1 Aa2 Ba1 Ba2 Ca1
# "1" "12" "123" "321" "444"
如果你可以稍微调整一下名称,你也可以使用 make.unique
:
setNames(purrr::flatten_chr(myList),make.unique(rep(names(myList), len),sep = ""))
# Aa Aa1 Ba Ba1 Ca
# "1" "12" "123" "321" "444"
如果我理解你是对的,你想在没有 unlist
的基础 R 中取消列出。你可以这样做:
res <- NULL
for (i in seq(names(myList))) {
nm <- names(myList)[i]
l <- myList[[nm]]
res<- c(res, setNames(l, paste0(nm, seq(l))))
}
res
# Aa1 Aa2 Ba1 Ba2 Ca1
# "1" "12" "123" "321" "444"
检查:
unlist(myList)
# Aa1 Aa2 Ba1 Ba2 Ca
# "1" "12" "123" "321" "444"
我也不得不承认不太理解你想要实现的目标,但如果你想要 unlist()
的结果而不明确使用该函数,你可以尝试 rapply()
- 虽然通过默认 unlist()
正在后台使用:
rapply(myList, f = function(x) x)
Aa1 Aa2 Ba1 Ba2 Ca
"1" "12" "123" "321" "444"
myList <- rep(0, 5)
myList <- list('Aa'= c("1","12"), 'Ba'= c("123","321"), 'Ca'= c("444"))
for (x in names(myList)) {
for (j in myList[x]) {
sample <- c(unlist(myList[j]))
} }
sample
在嵌套列表中,想要将此列表转换为具有
的向量"for"
循环
我不知道如何在不定义函数的情况下提取值。
sample <- c(unlist(myList[x])
执行“unlist”只需使用其他代码即可完成工作
> sample
NULL
但我想要全部,而这个不是这样的。有基础版吗?无需快速或简单。只有基本的 R 分发包,
alternative of "unlist"
如果可能的话。
我需要类似的东西
Aa1 Aa2 Ba1 Ba2 Ca1 "1" "12" "123" "444"
我想我不需要
myList <- rep(0, 5)
我可能不太理解你的问题,但不是
sample <- unlist(myList)
# Aa1 Aa2 Ba1 Ba2 Ca
# "1" "12" "123" "321" "444"
或
sample <- structure(unlist(myList))
# Aa1 Aa2 Ba1 Ba2 Ca
# "1" "12" "123" "321" "444"
实现你想要的?
不太确定为什么要避免 unlist
,但这里有一个替代方案 flatten_chr
。
len <- lengths(myList)
setNames(purrr::flatten_chr(myList),paste0(rep(names(myList),len),sequence(len)))
# Aa1 Aa2 Ba1 Ba2 Ca1
# "1" "12" "123" "321" "444"
如果你可以稍微调整一下名称,你也可以使用 make.unique
:
setNames(purrr::flatten_chr(myList),make.unique(rep(names(myList), len),sep = ""))
# Aa Aa1 Ba Ba1 Ca
# "1" "12" "123" "321" "444"
如果我理解你是对的,你想在没有 unlist
的基础 R 中取消列出。你可以这样做:
res <- NULL
for (i in seq(names(myList))) {
nm <- names(myList)[i]
l <- myList[[nm]]
res<- c(res, setNames(l, paste0(nm, seq(l))))
}
res
# Aa1 Aa2 Ba1 Ba2 Ca1
# "1" "12" "123" "321" "444"
检查:
unlist(myList)
# Aa1 Aa2 Ba1 Ba2 Ca
# "1" "12" "123" "321" "444"
我也不得不承认不太理解你想要实现的目标,但如果你想要 unlist()
的结果而不明确使用该函数,你可以尝试 rapply()
- 虽然通过默认 unlist()
正在后台使用:
rapply(myList, f = function(x) x)
Aa1 Aa2 Ba1 Ba2 Ca
"1" "12" "123" "321" "444"