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.
我有关于创建矢量的问题。如果我做< - 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? (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)
我可以尝试做5 *(1:20),但有更短的路吗? (因为在这种情况下,我需要知道整个长度(100),然后除以间隔(5)的大小得到20)
2 个解决方案
#1
52
In R the equivalent function is seq
and you can use it with the option by
:
在R中,等效函数是seq,您可以使用以下选项:
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
.
除此之外,您还可以使用其他选项,例如length.out和along.with。
length.out: If you want to get a total of 10 numbers between 0 and 1, for example:
length.out:如果你想在0和1之间得到总共10个数字,例如:
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).
along.with:它提供您提供的矢量的长度作为输入,并提供1:length(输入)的向量。
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. From the documentation for ?seq
虽然不使用along.with选项,但建议在这种情况下使用seq_along。来自?seq的文档
seq
is generic, and only the default method is described here. 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 useseq_along
in that case.seq是通用的,此处仅描述默认方法。请注意,无论参数名称如何,它都会调度第一个参数的类。如果仅使用一个参数来调用它可能会产生意想不到的后果,并且在这种情况下使用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.
希望这可以帮助。
#2
0
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
#1
52
In R the equivalent function is seq
and you can use it with the option by
:
在R中,等效函数是seq,您可以使用以下选项:
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
.
除此之外,您还可以使用其他选项,例如length.out和along.with。
length.out: If you want to get a total of 10 numbers between 0 and 1, for example:
length.out:如果你想在0和1之间得到总共10个数字,例如:
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).
along.with:它提供您提供的矢量的长度作为输入,并提供1:length(输入)的向量。
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. From the documentation for ?seq
虽然不使用along.with选项,但建议在这种情况下使用seq_along。来自?seq的文档
seq
is generic, and only the default method is described here. 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 useseq_along
in that case.seq是通用的,此处仅描述默认方法。请注意,无论参数名称如何,它都会调度第一个参数的类。如果仅使用一个参数来调用它可能会产生意想不到的后果,并且在这种情况下使用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.
希望这可以帮助。
#2
0
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