Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中创建负下标向量_R - Fatal编程技术网

如何在R中创建负下标向量

如何在R中创建负下标向量,r,R,我想创建一个向量,允许负索引,比如-100到100。这意味着执行G[-50]将选择位置-50处的元素,而不是除-50处之外的所有元素。你们能帮我吗。 谢谢 可以使用命名向量 x <- 1:201 names(x) <- c(-100:100) x["-100"] -100 1 x您可以使用命名向量 x <- 1:201 names(x) <- c(-100:100) x["-100"] -100 1 xR中的数字索引必须是正的,因此无法执行您想

我想创建一个向量,允许负索引,比如-100到100。这意味着执行G[-50]将选择位置-50处的元素,而不是除-50处之外的所有元素。你们能帮我吗。
谢谢

可以使用命名向量

x <- 1:201
names(x) <- c(-100:100)
x["-100"]

-100 
   1 

x您可以使用命名向量

x <- 1:201
names(x) <- c(-100:100)
x["-100"]

-100 
   1 

xR中的数字索引必须是正的,因此无法执行您想要的操作。不过,以下是一些解决办法:

  • 如果您知道最小值是多少(即索引可以从
    -100
    100
    ),只需将该数字(
    +1
    )添加到索引中即可。因此,您没有使用
    df[-100]
    ,而是使用
    df[1]

  • 可以将正索引和负索引拆分为列表中的两个向量。因此,使用
    df[-50]
    而不是
    df[[negative]][50]

  • 请参见此示例:

    df <- list('pos' = c(1, 2, 3, 4, 5),
               'neg' = c(-1, -2, -3, -4, -5))
    
    neg_index <- function(df, i) {
        if (i > 0) {
            return(df[['pos']][i])
        } else if (i < 0) {
            return(df[['neg']][abs(i)])
        } else {
            return(NULL)
        }
    }
    
    neg_index(df, -2)
    [1] -2
    
    neg_index(df, 4)
    [1] 4
    

    dfR中的数字索引必须是正的,所以没有办法做你想做的事。不过,以下是一些解决办法:

  • 如果您知道最小值是多少(即索引可以从
    -100
    100
    ),只需将该数字(
    +1
    )添加到索引中即可。因此,您没有使用
    df[-100]
    ,而是使用
    df[1]

  • 可以将正索引和负索引拆分为列表中的两个向量。因此,使用
    df[-50]
    而不是
    df[[negative]][50]

  • 请参见此示例:

    df <- list('pos' = c(1, 2, 3, 4, 5),
               'neg' = c(-1, -2, -3, -4, -5))
    
    neg_index <- function(df, i) {
        if (i > 0) {
            return(df[['pos']][i])
        } else if (i < 0) {
            return(df[['neg']][abs(i)])
        } else {
            return(NULL)
        }
    }
    
    neg_index(df, -2)
    [1] -2
    
    neg_index(df, 4)
    [1] 4
    

    df这样做很奇怪。您可以决定创建类来完成此任务。尽管我不建议您继续这样做:因此,我将称该类为“怪异的”

    weird = function(x,pos = NULL){
      stopifnot(length(x)%%2==1)
      s = seq_along(x)
      names(x) = s-floor(median(s))
      class(x) = "weird"
      x
    }
    print.weird = function(x) print(c(unname(x)))
    `[.weird` = function(i,j) unname(.Primitive("[")(unclass(i),as.character(j)))
    
    
    
    (s = weird(-10:10))
     [1] -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6   7
    [19]   8   9  10
    
    正如你所看到的,这只是一个-10:10的法向量,你看不到的是这个
    s
    的类是
    怪异的
    。您可以通过执行
    class

    现在只要做:

    s[-1]
    [1] -1
    > s[-10]
    [1] -10
    > s[0]
    [1] 0
    > s[3]
    [1] 3
    

    此外,如果需要使用此选项,则必须指定索引
    0
    应位于何处。例如
    m=1:5
    m[0]
    应该是什么?然后可以相应地更改函数

    这样做很奇怪。您可以决定创建类来完成此任务。尽管我不建议您继续这样做:因此,我将称该类为“怪异的”

    weird = function(x,pos = NULL){
      stopifnot(length(x)%%2==1)
      s = seq_along(x)
      names(x) = s-floor(median(s))
      class(x) = "weird"
      x
    }
    print.weird = function(x) print(c(unname(x)))
    `[.weird` = function(i,j) unname(.Primitive("[")(unclass(i),as.character(j)))
    
    
    
    (s = weird(-10:10))
     [1] -10  -9  -8  -7  -6  -5  -4  -3  -2  -1   0   1   2   3   4   5   6   7
    [19]   8   9  10
    
    正如你所看到的,这只是一个-10:10的法向量,你看不到的是这个
    s
    的类是
    怪异的
    。您可以通过执行
    class

    现在只要做:

    s[-1]
    [1] -1
    > s[-10]
    [1] -10
    > s[0]
    [1] 0
    > s[3]
    [1] 3
    
    此外,如果需要使用此选项,则必须指定索引
    0
    应位于何处。例如
    m=1:5
    m[0]
    应该是什么?然后您可以相应地更改功能

    您可以使用

    >库(hashmap)
    >H[-50]]
    [1] 51
    
    您可以使用

    >库(hashmap)
    >H[-50]]
    [1] 51