简体   繁体   English

在r中生成序列大于1但小于n的向量

[英]Generate a vector of sequence greater than 1 but less than n in r

How do I generate a vector of sequence in this range 1<i<n that is the number contained in the vector will be a positive integer greater than 1, but less than n.如何在此范围内生成序列向量1<i<n即向量中包含的数字将是大于 1 但小于 n 的正 integer。 Here is what I tried bellow:这是我在下面尝试的:

n <- 10
my_seq <- seq(from => 1, to =< n)

It gave me this error:它给了我这个错误:

Error: unexpected '>' in "my_seq <- seq(from =>"

my expected output should be我预期的 output 应该是

[1] 2 3 4 5 6 7 8 9

Depending on which type of vectors you need.取决于您需要哪种类型的载体。 Below are some examples:下面是一些例子:

  • If you want to have ascend sequence (without duplicates)如果你想有升序(没有重复)
seq(n-2)+1
# [1]  2  3  4  5  6  7  8  9
  • If you want to shuffle the values 2 to n-2 :如果要将值2改组为n-2
sample(n-2)+1
# [1]  6  7  9  5  8  4  2  3
  • If you need random integers that allow duplicates如果您需要允许重复的随机整数
sample(n-2,replace = TRUE)+1
# [1]  5  2  8  9  4  3  6  9 

You could generate the sequence using您可以使用生成序列

n <- 10
2:(n-1)
#[1] 2 3 4 5 6 7 8 9

OR或者

seq(2, n - 1)

You can also do:你也可以这样做:

tail(head(1:n, -1), -1)

[1] 2 3 4 5 6 7 8 9

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM