官方帮助文档如下写的:

Usage

rep(x, ...)

rep.int(x, times)

rep_len(x, length.out)

Arguments

x

a vector (of any mode including a list) or a factor or (for rep only) a POSIXct or POSIXlt or Date object; or an S4 object containing such an object.

...

further arguments to be passed to or from other methods. For the internal default method these can include:

times

A integer vector giving the (non-negative) number of times to repeat each element if of length length(x), or to repeat the whole vector if of length 1. Negative or NA values are an error.

length.out

non-negative integer. The desired length of the output vector. Other inputs will be coerced to an integer vector and the first element taken. Ignored if NA or invalid.

each

non-negative integer. Each element of x is repeated each times. Other inputs will be coerced to an integer vector and the first element taken. Treated as 1 if NA or invalid.

times

see ....

length.out

non-negative integer: the desired length of the output vector.

rep函数有4个参数:x向量或者类向量的对象,each:x元素每个重复次数,times:each后的向量的处理,如果times是单个值,则each后的值整体重复times次数,如果是x each后的向量相等长度的向量,则对each后的每个元素重复times同一位置的元素的次数,否则会报错;length.out指times处理后的向量最终输出的长度,如果长于生成的向量,则补齐。也就是说rep会先处理each参数,生成一个向量X1,然后times再对X1进行处理生成X2,length.out在对X2进行处理生成最终输出的向量X3。下面是示例:

> rep(1:4,times=c(1,2,3,4))  #与向量x等长times模式
[1] 1 2 2 3 3 3 4 4 4 4
> rep(1:4,times=c(1,2,3))  #非等长模式,出现错误
Error in rep(1:4, times = c(1, 2, 3)) : invalid 'times' argument
> rep(1:4,each=2,times=c(1,2,3,4)) #还是非等长模式,因为each后的向量有8位,而不是4位
Error in rep(1:4, each = 2, times = c(1, 2, 3, 4)) :
invalid 'times' argument
> rep(1:4,times=c(1,2,3,4))  #等长模式,我写重了o(╯□╰)o
[1] 1 2 2 3 3 3 4 4 4 4
> rep(1:4,times=c(1,2,3,4),each=3) #重复的例子啊,莫拍我
Error in rep(1:4, times = c(1, 2, 3, 4), each = 3) :
invalid 'times' argument
> rep(1:4,each=2,times=1:8)   #正确值,times8位长度向量
[1] 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
> rep(1:4,each=2,times=1:8,len=3)   #len的使用,循环补齐注意下
[1] 1 1 2
> rep(1:4,each=2,times=3)   #先each后times
[1] 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4
>rep函数完毕!

R中rep函数的使用的更多相关文章

  1. R中unlist函数的使用

    买的书里面实例讲的不清不楚,所以看帮助文档了 用法:unlist(x, recursive = TRUE, use.names = TRUE) 帮助文档讲x可以是向量或者列表,如果是向量,则原样返回, ...

  2. R中的par()函数的参数

    把R中par()函数的主要参数整理了一下(另外本来还整理了每个参数的帮助文档中文解释,但是太长,就分类之后,整理为图表,excel不便放上来,就放了这些表的截图)

  3. R中实现脚本调用,以及函数调用

    R中实现脚本调用,以及函数调用 这里的列子是test.R调用mysql_con.R中的函数 mysql_con.R # 使用RMySQL操作数据库 # 载入DBI和RMySQL包 #library(D ...

  4. [R]在dplyr函数的基础上编写函数-(3)tidyeval

    dplyr的优点很明显,数据框操作简洁,如filter(df, x == 1, y == 2, z == 3)等于df[df$x == 1 & df$y ==2 & df$z == 3 ...

  5. j解决sparkr中使用某些r的原生函数 发生错误Error: class(objId) == "jobj" is not TRUE的问题

    Create table function in Spark in R not working João_Andre  (3) 询问的问题 | 2016年12月10日 06:03BLUEMIXRSPA ...

  6. R中的apply族函数和多线程计算

    一.apply族函数 1.apply  应用于矩阵和数组 # apply # 1代表行,2代表列 # create a matrix of 10 rows x 2 columns m <- ma ...

  7. R中的name命名系列函数总结

    本文原创,转载请注明出处,本人Q1273314690 R中关于给行列赋名称的函数有 dimnames,names,rowname,colname,row.names 这五个函数,初学的时候往往分不清楚 ...

  8. 总结——R中查看属性的函数

    本文原创,转载注明出处,本人Q1273314690 R中知道一个变量的主要内容和结构,对我们编写代码是很重要的,也可以帮我们避免很多错误. 但是,R中有好几个关于属性查看的函数,我们往往不知道什么时候 ...

  9. R中的sample函数

    今天介绍一些运算函数,它们的使用很简单,没有什么难度,但是也会用的着. 在医学统计学或者流行病学里的现场调查.样本选择经常会提到一个词:随机抽样.随机抽样是为了保证各比较组之间均衡性的一个很重要的方法 ...

随机推荐

  1. axios中设置Content-Type不生效的问题

    Api:axios(config): config中无data字段时,headers里的Content-Type无效果,这应该出于优化的层面,此时的Content-Length=0,无需向服务端提供C ...

  2. 洛谷 P1194 飞扬的小鸟 题解

    题面 这道题是一道隐藏的比较深的DP(我太蒟蒻了!) 设f[i][j]表示到第i列时高度为j的最少步数是多少: 求上升时的方案就是一个完全背包!,求下降时的方案就是一个01背包: 然后处理边界就能A掉 ...

  3. python3—廖雪峰之练习(一)

    变量练习 小明的成绩从去年的72分提升到今年的85分,请计算小明成绩提升的百分点.并用 字符串格式化显示出'xx.x%',只保留小数点后一位: s1 = 72 s2 = 85 r = (85-72)/ ...

  4. DataFrame.to_dict(orient='dict')

    DataFrame.to_dict(orient=’dict’) >>> df = pd.DataFrame({'name':[1,2,3],"class":[1 ...

  5. 模板 - 强连通分量/割点/桥 - Tarjan

    int dfn[N], low[N], dfncnt, s[N], tp; int scc[N], sc; // 结点 i 所在 scc 的编号 int sz[N]; // 强连通 i 的大小 voi ...

  6. AGC015E Mr.Aoki Incubator

    atcoder luogu 首先可以考虑给一个人\(A\)染色.其他人被染色,要么被本来在后面的速度更快的人染色,要么被在前面的更慢的人染色.然后假设一个速度比最开始那个人慢的人\(B\)最后被染色了 ...

  7. Linux学习--第八天--acl、SetUID、SetGID、chattr、lsattr、sudo

    acl权限 文件只能有一个所属组 acl就是不管用户什么组了,直接针对某个文件给他特定权限. acl需要所在分区文件系统的支持. df -h #查看分区 dumpe2fs -h /dev/sda3 # ...

  8. Centos6 修复grub损坏故障

    1.查看系统中的/boot/grub/grub.conf # grub.conf generated by anaconda # # Note that you do not have to reru ...

  9. win10将mongodb加入系统服务,官方源码报错问题记录

    进入C:\Program Files\MongoDB\Server\3.6目录下 1.编写配置文件mongodb.cfg: dbpath=D:\MongoDB\data\db #数据库路径 logpa ...

  10. Transposed Convolution 反卷积

    Transposed convolutions也称作fractionally strided convolutions(本人比较喜欢这个称呼,比较直观),Upconvolution,deconvolu ...