将整数列表总结为 R 中的两列
Summary a list of integers into two columns in R
这是我得到的(列表):
>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..)
..如果这是聪明的方法,我该如何防止重命名?
糟糕!好的,搞定了:
res <- cbind(
rep(1:length(indexes), vapply(indexes,length,1)),
c(indexes,recursive=TRUE)
)
.. 如果有人找到更好的方法,请告诉我,然后:)
这是我得到的(列表):
>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..)
..如果这是聪明的方法,我该如何防止重命名?
糟糕!好的,搞定了:
res <- cbind(
rep(1:length(indexes), vapply(indexes,length,1)),
c(indexes,recursive=TRUE)
)
.. 如果有人找到更好的方法,请告诉我,然后:)