2017-03-02 87 views
1

我有一个列表输出看起来像这样的片段:如何变换成列表查找表

[[1]] 
[1] 109 

[[2]] 
integer(0) 

[[3]] 
[1] 80 

有没有办法把它改造成这种格式?

C1 C2 
1 109 
2 0 (or NA) 
3 80 

不知道在哪里甚至开始......如果我选择不公开它,我发现我失去了我的整数(0)的位置,这是非常重要,我保持。

+0

http://stackoverflow.com/questions/17388096/convert-list-with-null-entres-to-data-frame-的可能重复做在-r – akrun

回答

5

随着as.numeric可以保留integer(0)NA

data.frame(C1 = seq(length(lst)), C2 = as.numeric(lst)) 

# C1 C2 
#1 1 109 
#2 2 NA 
#3 3 80 
+1

也'seq_along(lst)'。 – lmo

2

L = list(109,integer(0),80) 
sapply(L, '[', 1:1) #OR sapply(L, function(a) a[1]) 
#[1] 109 NA 80 

#If you need a data.frame 

data.frame(cbind(1:length(L),lapply(L, '[', 1:1))) 
# X1 X2 
#1 1 109 
#2 2 NA 
#3 3 80 
+1

,让我通过! – val

+0

我不熟悉'['你能解释吗?我假设1:1只是引用每个列表中的第一个元素。 – val

+1

[Here](http://stackoverflow.com/a/19261054/7128934)是一个很好的解释 –

1

我们可以通过stack

stack(setNames(lapply(lst, `length<-`, 1), seq_along(lst)))[2:1]