我认为在Matlab中我们可以做到1:5:100,我们如何使用R做到这一点?我可以尝试做5*(1:20),但是有更短的方法吗? (因为在这种情况下,我需要知道整个长度(100),然后除以间隔(5)的大小才能得到20)解决方案在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除了by,您还可以使用其他选项,例如length.out和along.with. length.out :如果您希望获得10个介于0和1之间的数字,例如:seq(0, 1, length.out = 10)# gives 10 equally spaced numbers from 0 to 1 一起:它以您提供的向量的长度作为输入,并从1:length(input)提供向量.seq(along.with=c(10,20,30))# [1] 1 2 3尽管在这种情况下,建议不要使用along.with选项,而建议使用seq_along.从?seq 的文档中 seq是通用的,此处仅描述默认方法.请注意,它会在 first 参数的类上分派,而与参数名称无关.如果仅使用一个参数来调用它,则可能会产生意想不到的后果.在这种情况下,使用seq_along更好. seq_along :而不是seq(along.with(.)) seq_along(c(10,20,30))# [1] 1 2 3希望这会有所帮助.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.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,100I think that in Matlab we can do 1:5:100, how do we do this using 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) 解决方案 In R the equivalent function is seq and you can use it with the option 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 100In addition to by you can also have other options such as length.out and along.with.length.out: If you want to get a total of 10 numbers between 0 and 1, for example:seq(0, 1, length.out = 10)# gives 10 equally spaced numbers from 0 to 1along.with: It takes the length of the vector you supply as input and provides a vector from 1:length(input).seq(along.with=c(10,20,30))# [1] 1 2 3Although, instead of using the along.with option, it is recommended to use seq_along in this case. From the documentation for ?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 use seq_along in that case.seq_along: Instead of seq(along.with(.))seq_along(c(10,20,30))# [1] 1 2 3Hope this helps. 这篇关于如何在R中创建具有特定间隔的向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!