简体   繁体   English

如何在R中创建具有特定间隔的向量?

[英]How do you create vectors with specific intervals in R?

I have a question about creating vectors. 我对创建向量有疑问。 If I do a <- 1:10 , "a" has the values 1,2,3,4,5,6,7,8,9,10. 如果我执行a <- 1:10 ,则“ a”的值为1,2,3,4,5,6,7,8,9,10。

My question is how do you create a vector with specific intervals between its elements. 我的问题是如何创建一个在其元素之间具有特定间隔的向量。 For example, I would like to create a vector that has the values from 1 to 100 but only count in intervals of 5 so that I get a vector that has the values 5,10,15,20,...,95,100 例如,我想创建一个向量,其值从1到100,但仅以5的间隔计数,这样我就得到一个向量,其值是5,10,15,20,...,95,100

I think that in Matlab we can do 1:5:100 , how do we do this using R? 我认为在Matlab中我们可以做到1:5:100 ,我们如何使用R做到这一点?

I could try doing 5*(1:20) but is there a shorter way? 我可以尝试做5*(1:20)但是有更短的方法吗? (since in this case I would need to know the whole length (100) and then divide by the size of the interval (5) to get the 20) (因为在这种情况下,我需要知道整个长度(100),然后除以间隔(5)的大小即可得出20)

In R the equivalent function is seq and you can use it with the option by : 在R中,等效函数是seq ,您可以通过以下选项将其与选项一起by

seq(from = 5, to = 100, by = 5)
# [1]   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75  80  85  90  95 100

In addition to by you can also have other options such as length.out and along.with . 除了by之外by您还可以使用其他选项,例如length.outalong.with

length.out : If you want to get a total of 10 numbers between 0 and 1, for example: length.out :如果您要获得10个介于0和1之间的数字,例如:

seq(0, 1, length.out = 10)
# gives 10 equally spaced numbers from 0 to 1

along.with : It takes the length of the vector you supply as input and provides a vector from 1:length(input). With.with :它将您提供的向量的长度作为输入,并从1:length(input)提供一个向量。

seq(along.with=c(10,20,30))
# [1] 1 2 3

Although, instead of using the along.with option, it is recommended to use seq_along in this case. 尽管在这种情况下,建议使用seq_along而不是使用加上along.with选项。 From the documentation for ?seq ?seq的文档中

seq is generic, and only the default method is described here. seq是通用的,此处仅描述默认方法。 Note that it dispatches on the class of the first argument irrespective of argument names. 请注意,它与第一个参数的类一起分派,而与参数名称无关。 This can have unintended consequences if it is called with just one argument intending this to be taken as along.with: it is much better to use seq_along in that case. 如果仅使用一个参数来调用它,这可能会产生意想不到的后果。在这种情况下,最好使用seq_along

seq_along: Instead of seq(along.with(.)) seq_along:代替seq(along.with(.))

seq_along(c(10,20,30))
# [1] 1 2 3

Hope this helps. 希望这可以帮助。

Use the code 使用代码

x = seq(0,100,5) #this means (starting number, ending number, interval)

the output will be 输出将是

[1]   0   5  10  15  20  25  30  35  40  45  50  55  60  65  70  75
[17]  80  85  90  95 100

Usually, we want to divide our vector into a number of intervals. 通常,我们希望将向量划分为多个间隔。 In this case, you can use a function where (a) is a vector and (b) is the number of intervals. 在这种情况下,可以使用以下函数,其中(a)是向量,(b)是间隔数。 (Let's suppose you want 4 intervals) (假设您想要4个间隔)

a <- 1:10
b <- 4

FunctionIntervalM <- function(a,b) {
  seq(from=min(a), to = max(a), by = (max(a)-min(a))/b)
}

FunctionIntervalM(a,b)
# 1.00  3.25  5.50  7.75 10.00

Therefore you have 4 intervals: 因此,您有4个间隔:

1.00 - 3.25 
3.25 - 5.50
5.50 - 7.75
7.75 - 10.00

You can also use a cut function 您还可以使用剪切功能

  cut(a, 4)

# (0.991,3.25] (0.991,3.25] (0.991,3.25] (3.25,5.5]   (3.25,5.5]   (5.5,7.75]  
# (5.5,7.75]   (7.75,10]    (7.75,10]    (7.75,10]   
#Levels: (0.991,3.25] (3.25,5.5] (5.5,7.75] (7.75,10]

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

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