data.table的索引列表

时间:2014-03-04 11:40:46

标签: r list data.table

假设后续的向量列表,

ll <- structure(list(
    `007` = NULL, `1908` = 2L, `1934` = NULL, `1948` = NULL, `333` = 4L, baloni = 17L, abate = 19L, abatjour = 18L, abbacchio = 20:32, abbeveraggio = 33L),
    .Names = c("007", "1908", "1934", "1948", "333", "abaloni", "abate", "abatjour", "abbacchio", "abbeveraggio"
))

其中每个代表一组位置(即指数)。

创建data.table的快速而智能的方法是什么,其中每一行都包含相应位置所属的列表名称?

因此,对于上一个对象ll,结果如下:

              V1
 1:           NA
 2:         1908
 3:           NA
 4:          333
 5:           NA
 6:           NA
 7:           NA
 8:           NA
 9:           NA
10:           NA
11:           NA
12:           NA
13:           NA
14:           NA
15:           NA
16:           NA
17:      abaloni
18:     abatjour
19:        abate
20:    abbacchio
21:    abbacchio
22:    abbacchio
23:    abbacchio
24:    abbacchio
25:    abbacchio
26:    abbacchio
27:    abbacchio
28:    abbacchio
29:    abbacchio
30:    abbacchio
31:    abbacchio
32:    abbacchio
33: abbeveraggio

一些注意事项:

  • 列出儿童的长度不一样
  • 不存在的职位必须填写NA

我认为解决方案方法主要包括恢复列表结构(索引与术语),但我当时无法达成解决方案。

一些帮助或建议?

谢谢。

2 个答案:

答案 0 :(得分:1)

这是一种方法:

# create matirx
mat <- do.call(rbind, lapply(seq_along(ll), function(x) 
  if (!is.null(ll[[x]]))cbind(ll[[x]], names(ll)[x])))

# create index
idx <- as.integer(mat[, 1])

# create vector
vec <- rep(NA_character_, max(idx))
vec[idx] <- mat[, 2]

# create data.table
library(data.table)
data.table(V1 = vec)

结果:

             V1
 1:           NA
 2:         1908
 3:           NA
 4:          333
 5:           NA
 6:           NA
 7:           NA
 8:           NA
 9:           NA
10:           NA
11:           NA
12:           NA
13:           NA
14:           NA
15:           NA
16:           NA
17:      abaloni
18:     abatjour
19:        abate
20:    abbacchio
21:    abbacchio
22:    abbacchio
23:    abbacchio
24:    abbacchio
25:    abbacchio
26:    abbacchio
27:    abbacchio
28:    abbacchio
29:    abbacchio
30:    abbacchio
31:    abbacchio
32:    abbacchio
33: abbeveraggio
              V1

答案 1 :(得分:1)

我只是遍历您的列表并使用set

d = data.table(rep(NA_character_, max(sapply(ll, max))))

for (i in seq_along(ll))
  if (!is.null(ll[[i]])) set(d, ll[[i]], 1L, names(ll)[i])