汇总R中两列的整数列表

时间:2015-03-04 17:00:19

标签: r list matrix integer names

这就是我所得到的(清单):

>head(indexes)
[[1]]
numeric(0)

[[2]]
[1] 12

[[3]]
[1] 13

[[4]]
[1] 2 3

[[5]]
[1] 25

[[6]]
[1] 26

> all(vapply(indexes, is.numeric, TRUE)) # (note that..)
[1] TRUE

..这就是我想要的(同样的信息):

>head(res,6)
     [,1] [,2]
[1,]    2   12
[2,]    3   13
[3,]    4    2
[4,]    4    3
[5,]    5   25
[6,]    6   36

有一种聪明的方法吗?

我尝试了一个命名列表的技巧:

names(indexes) <- 1:lenght(indexes)
res <- c(indexes, recursive=TRUE)
res <- cbind(as.integer(names(res)), res)

但是R(这样一个该死的孩子!)通过以模棱两可的方式重命名相同的行来打破一切:

>head(res)
      res
2   2   2
3   3   3
41 41   2
42 42   3
5   5   5
6   6   6

# ... (think about what happens around lines 3675.. 41158..)

..如果这是一个聪明的方法,我该如何阻止重命名?

1 个答案:

答案 0 :(得分:0)

佬!好的,钉了它:

res <- cbind(
  rep(1:length(indexes), vapply(indexes,length,1)),
   c(indexes,recursive=TRUE)
)

..告诉我,如果有人找到了更好的方法,那么:)