Subscript type for remove an Array column from index in R

huangapple go评论94阅读模式
英文:

Subscript type for remove an Array column from index in R

问题

可以用作从n维数组中删除列的索引参数是一个矩阵(matrix)。以下是您提供的R代码的翻译部分:

  1. deleteCol <- function(array, col_to_del, in_dim) {
  2. index <- vector(mode = 'list', length = length(dim(array)))
  3. index[in_dim] <- -col_to_del
  4. return(array[index, drop = FALSE])
  5. }

希望这对您有所帮助。

英文:

What kind of object can I use as an index argument to remove columns from an n-dim array?

  1. deleteCol &lt;- function(array,col_to_del,in_dim){
  2. index &lt;- vector(mode=&#39;list&#39;, length=length(dim(array)))
  3. index[in_dim] &lt;- -col_to_del
  4. return(array[index, drop = FALSE])
  5. }

I want to make a function that removes a specific column from an array, and I want to make this work for any n-dimension arrays. I already know that lists aren't accepted as index arguments, but matrix does. The code is just to show what I want to do. I don't find that an index matrix does the job because I can't enter negative or NULL values required for indexes like " , , ... , -n, ... ,".

I try to make an index matrix, but it doesn't accept negative elements, nor does it have an element to leave an empty position.

答案1

得分: 0

Oh, codey-wodey! This looks like a puzzling language to me, but I'll try my best to help!

Here's the code part without translation:

  1. a <- array(1:32, dim=c(2,2,2,2))
  2. dim(a)
  3. ##[1] 2 2 2 2
  4. qeo <- list(quote(expr=))
  5. args <- c(qeo, -1, rep(qeo, length(dim(a))-2), drop = FALSE)
  6. out <- do.call(`[`, c(list(a), args))
  7. dim(out)
  8. ##[1] 2 1 2 2
  9. removecol <- function(array, col_to_del, in_dim){
  10. qeo <- list(quote(expr=))
  11. qeo <- rep(qeo, length(dim(array)))
  12. qeo[in_dim] <- -col_to_del
  13. args <- c(qeo, drop = FALSE)
  14. do.call(`[`, c(list(array), args))
  15. }

I hope that helps, even though it's like a magical incantation from a wizard's spellbook! 😄✨

英文:

This is extraordinarily ugly, but here's a way to pass the empty arguments to drop columns programmatically from an n-dimensional array:

  1. a &lt;- array(1:32, dim=c(2,2,2,2))
  2. dim(a)
  3. ##[1] 2 2 2 2
  4. qeo &lt;- list(quote(expr=))
  5. args &lt;- c(qeo, -1, rep(qeo, length(dim(a))-2), drop = FALSE)
  6. out &lt;- do.call(`[`, c(list(a), args))
  7. dim(out)
  8. ##[1] 2 1 2 2

Put into a flexible function by @magavich, from the comments:

  1. removecol &lt;- function(array, col_to_del, in_dim){
  2. qeo &lt;- list(quote(expr=))
  3. qeo &lt;- rep(qeo, length(dim(array)))
  4. qeo[in_dim] &lt;- -col_to_del
  5. args &lt;- c(qeo, drop = FALSE)
  6. do.call(`[`, c(list(array), args))
  7. }

huangapple
  • 本文由 发表于 2023年7月17日 11:08:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定