R: are there built-in functions to sort lists?

时间:2020-12-26 18:09:00

in R I have produced the following list L:

在R我已经产生了以下列表L:

>L
[[1]]
[1] "A" "B" "C"  

[[2]]
[1] "D"  

[[3]]
[1] NULL

I would like to manipulate the list L arriving at a database df like

我想操纵列表L到达数据库df之类的

>df
    df[,1] df[,2]
       "A"      1 
       "B"      1
       "C"      1 
       "D"      2

where the 2nd column gives the position in the list L of the corresponding element in column 1. My question is: is(are) there a() built-in R function(s) which can do this manipulation quickly? I can do it using "brute force", but my solution does not scale well when I consider much bigger lists.

第二列给出了第1列中相应元素的列表L中的位置。我的问题是:是(是)有一个()内置R函数可以快速执行此操作吗?我可以使用“暴力”来做到这一点,但是当我考虑更大的列表时,我的解决方案不能很好地扩展。

I thank you all!

谢谢大家!

2 个解决方案

#1


3  

You'll get a warning because of your NULL value, but you can use stack if you give your list items names:

由于您的NULL值,您将收到警告,但如果您提供列表项名称,则可以使用堆栈:

L <- list(c("A", "B", "C"), "D", NULL)
stack(setNames(L, seq_along(L)))
#   values ind
# 1      A   1
# 2      B   1
# 3      C   1
# 4      D   2
# Warning message:
# In stack.default(setNames(L, seq_along(L))) :
#   non-vector elements will be ignored

If the warning displeases you, you can, of course, run stack on the non-NULL elements, but do it after you name your list elements so that the "ind" column reflects the correct value.

如果警告让您不高兴,您当然可以在非NULL元素上运行堆栈,但在命名列表元素后执行此操作,以便“ind”列反映正确的值。

I'll show in 2 steps just for clarity:

为了清楚起见,我将分两步显示:

names(L) <- seq_along(L)
stack(L[!sapply(L, is.null)])

Similarly, if you've gotten rid of the NULL list elements, you can use melt from "reshape2". You don't gain anything in brevity, and I'm not sure that you gain anything in efficiency either, but I thought I'd share it as an option.

同样,如果你已经摆脱了NULL列表元素,你可以使用“reshape2”中的melt。你没有得到任何简洁的东西,我也不确定你在效率上有什么收获,但我想我会分享它作为一种选择。

library(reshape2)
names(L) <- seq_along(L)
melt(L[!sapply(L, is.null)])

#2


2  

Ananda's answer is seemingly better than this, but I'll put it up anyway:

阿南达的回答似乎比这更好,但无论如何我都会提出来:

> cbind(unlist(L), rep(1:length(L), sapply(L, length)))
     [,1] [,2]
[1,] "A"  "1" 
[2,] "B"  "1" 
[3,] "C"  "1" 
[4,] "D"  "2"

#1


3  

You'll get a warning because of your NULL value, but you can use stack if you give your list items names:

由于您的NULL值,您将收到警告,但如果您提供列表项名称,则可以使用堆栈:

L <- list(c("A", "B", "C"), "D", NULL)
stack(setNames(L, seq_along(L)))
#   values ind
# 1      A   1
# 2      B   1
# 3      C   1
# 4      D   2
# Warning message:
# In stack.default(setNames(L, seq_along(L))) :
#   non-vector elements will be ignored

If the warning displeases you, you can, of course, run stack on the non-NULL elements, but do it after you name your list elements so that the "ind" column reflects the correct value.

如果警告让您不高兴,您当然可以在非NULL元素上运行堆栈,但在命名列表元素后执行此操作,以便“ind”列反映正确的值。

I'll show in 2 steps just for clarity:

为了清楚起见,我将分两步显示:

names(L) <- seq_along(L)
stack(L[!sapply(L, is.null)])

Similarly, if you've gotten rid of the NULL list elements, you can use melt from "reshape2". You don't gain anything in brevity, and I'm not sure that you gain anything in efficiency either, but I thought I'd share it as an option.

同样,如果你已经摆脱了NULL列表元素,你可以使用“reshape2”中的melt。你没有得到任何简洁的东西,我也不确定你在效率上有什么收获,但我想我会分享它作为一种选择。

library(reshape2)
names(L) <- seq_along(L)
melt(L[!sapply(L, is.null)])

#2


2  

Ananda's answer is seemingly better than this, but I'll put it up anyway:

阿南达的回答似乎比这更好,但无论如何我都会提出来:

> cbind(unlist(L), rep(1:length(L), sapply(L, length)))
     [,1] [,2]
[1,] "A"  "1" 
[2,] "B"  "1" 
[3,] "C"  "1" 
[4,] "D"  "2"