R - 生成数字序列 [英] R - Generate a sequence of numbers

查看:65
本文介绍了R - 生成数字序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建 6 个案例的序列,但间隔为 144 个案例.

I am trying to create sequences of number of 6 cases, but with 144 cases intervals.

以这个为例

c(1:6, 144:149, 288:293)

1   2   3   4   5   6 144 145 146 147 148 149 288 289 290 291 292 293

我怎么能用

seq 

还是其他功能?

推荐答案

我发现 sequence 函数在这种情况下很有帮助.如果您的数据采用如下结构:

I find the sequence function to be helpful in this case. If you had your data in a structure like this:

(info <- data.frame(start=c(1, 144, 288), len=c(6, 6, 6)))
#   start len
# 1     1   6
# 2   144   6
# 3   288   6

那么你可以在一行中做到这一点:

then you could do this in one line with:

sequence(info$len) + rep(info$start-1, info$len)
#  [1]   1   2   3   4   5   6 144 145 146 147 148 149 288 289 290 291 292 293

请注意,即使您组合的序列长度不同,此解决方案也能奏效.

Note that this solution works even if the sequences you're combining are different lengths.

这篇关于R - 生成数字序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆