优雅的索引到向量/矩阵的末尾

发布于 2024-12-05 20:57:07 字数 344 浏览 0 评论 0原文

R 中是否可以说 - 我想要从位置 i 到向量/矩阵末尾的所有索引? 假设我想要一个从第三列开始的子矩阵。我目前只知道这样:

A = matrix(rep(1:8, each = 5), nrow = 5) # just generate some example matrix...

A[,3:ncol(A)] # get submatrix from 3rd column onwards

但是我真的需要写ncol(A)吗?有没有什么优雅的方式来表达“从第三栏开始”?像A[,3:]之类的东西? (或A[,3:...])?

Is it possible in R to say - I want all indices from position i to the end of vector/matrix?
Say I want a submatrix from 3rd column onwards. I currently only know this way:

A = matrix(rep(1:8, each = 5), nrow = 5) # just generate some example matrix...

A[,3:ncol(A)] # get submatrix from 3rd column onwards

But do I really need to write ncol(A)? Isn't there any elegant way how to say "from the 3rd column onwards"? Something like A[,3:]? (or A[,3:...])?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

时光病人 2024-12-12 20:57:07

有时,告诉 R 你不想要什么会更容易。换句话说,使用负索引从矩阵中排除列:

以下是两种产生相同结果的替代方法:

A[, -(1:2)]
A[, -seq_len(2)]

结果:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    4    5    6    7    8
[2,]    3    4    5    6    7    8
[3,]    3    4    5    6    7    8
[4,]    3    4    5    6    7    8
[5,]    3    4    5    6    7    8

但要按要求回答您的问题:使用 ncol 查找列数。 (类似地,可以使用 nrow 来查找行数。)

A[, 3:ncol(A)]

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    4    5    6    7    8
[2,]    3    4    5    6    7    8
[3,]    3    4    5    6    7    8
[4,]    3    4    5    6    7    8
[5,]    3    4    5    6    7    8

Sometimes it's easier to tell R what you don't want. In other words, exclude columns from the matrix using negative indexing:

Here are two alternative ways that both produce the same results:

A[, -(1:2)]
A[, -seq_len(2)]

Results:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    4    5    6    7    8
[2,]    3    4    5    6    7    8
[3,]    3    4    5    6    7    8
[4,]    3    4    5    6    7    8
[5,]    3    4    5    6    7    8

But to answer your question as asked: Use ncol to find the number of columns. (Similarly there is nrow to find the number of rows.)

A[, 3:ncol(A)]

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    4    5    6    7    8
[2,]    3    4    5    6    7    8
[3,]    3    4    5    6    7    8
[4,]    3    4    5    6    7    8
[5,]    3    4    5    6    7    8
在巴黎塔顶看东京樱花 2024-12-12 20:57:07

对于行(不是您的示例中的列),可以使用 head()tail()

A <- matrix(rep(1:8, each = 5), nrow = 5)
tail(A, 3)

几乎相同

A[3:dim(A)[1],]

(打印的行名/索引都不同)。

这些也适用于向量和数据框:

> tail(1:10, 4)
[1]  7  8  9 10
> tail(data.frame(A = 1:5, B = 1:5), 3)
  A B
3 3 3
4 4 4
5 5 5

对于列版本,您可以调整 tail(),但这有点棘手。我想知道 NROW()NCOL() 在这里是否有用,而不是 dim()?:

> A[, 3:NCOL(A)]
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    4    5    6    7    8
[2,]    3    4    5    6    7    8
[3,]    3    4    5    6    7    8
[4,]    3    4    5    6    7    8
[5,]    3    4    5    6    7    8

或者翻转它的头并与其向 R 索取东西,不如要求它放弃东西。这是一个封装了这个的函数:

give <- function(x, i, dimen = 1L) {
    ind <- seq_len(i-1)
    if(isTRUE(all.equal(dimen, 1L))) { ## rows
        out <- x[-ind, ]
    } else if(isTRUE(all.equal(dimen, 2L))) { ## cols
        out <- x[, -ind]
    } else {
        stop("Only for 2d objects")
    }
    out
}

> give(A, 3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    4    5    6    7    8
[2,]    1    2    3    4    5    6    7    8
[3,]    1    2    3    4    5    6    7    8
> give(A, 3, dimen = 2)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    4    5    6    7    8
[2,]    3    4    5    6    7    8
[3,]    3    4    5    6    7    8
[4,]    3    4    5    6    7    8
[5,]    3    4    5    6    7    8

For rows (not columns as per your example) then head() and tail() could be utilised.

A <- matrix(rep(1:8, each = 5), nrow = 5)
tail(A, 3)

is almost the same as

A[3:dim(A)[1],]

(the rownames/indices printed are different is all).

Those work for vectors and data frames too:

> tail(1:10, 4)
[1]  7  8  9 10
> tail(data.frame(A = 1:5, B = 1:5), 3)
  A B
3 3 3
4 4 4
5 5 5

For the column versions, you could adapt tail(), but it is a bit trickier. I wonder if NROW() and NCOL() might be useful here, rather than dim()?:

> A[, 3:NCOL(A)]
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    4    5    6    7    8
[2,]    3    4    5    6    7    8
[3,]    3    4    5    6    7    8
[4,]    3    4    5    6    7    8
[5,]    3    4    5    6    7    8

Or flip this on its head and instead of asking R for things, ask it to drop things instead. Here is a function that encapsulates this:

give <- function(x, i, dimen = 1L) {
    ind <- seq_len(i-1)
    if(isTRUE(all.equal(dimen, 1L))) { ## rows
        out <- x[-ind, ]
    } else if(isTRUE(all.equal(dimen, 2L))) { ## cols
        out <- x[, -ind]
    } else {
        stop("Only for 2d objects")
    }
    out
}

> give(A, 3)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    2    3    4    5    6    7    8
[2,]    1    2    3    4    5    6    7    8
[3,]    1    2    3    4    5    6    7    8
> give(A, 3, dimen = 2)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    3    4    5    6    7    8
[2,]    3    4    5    6    7    8
[3,]    3    4    5    6    7    8
[4,]    3    4    5    6    7    8
[5,]    3    4    5    6    7    8
南风几经秋 2024-12-12 20:57:07

您可以使用以下指令:

A[, 3:length(A[, 1])]

You can use the following instruction:

A[, 3:length(A[, 1])]
如梦初醒的夏天 2024-12-12 20:57:07

针对同一件事的 dplyr 可读更新方法:

A %>% as_tibble() %>%
  select(-c(V1,V2))
  
A %>% as_tibble() %>%
  select(V3:ncol(A))

A dplyr readable renewed approach for the same thing:

A %>% as_tibble() %>%
  select(-c(V1,V2))
  
A %>% as_tibble() %>%
  select(V3:ncol(A))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文