官方帮助文档如下写的:

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. MongoDB和Redis的区别

    1).内存管理机制 a.Redis的数据全部存储在内存当中,会定期写入到磁盘当中,当内存不够用时, 可以选择指定的LRU(最近最少使用算法)的算法删除数据: b.MongoDB数据存在内存,有Linu ...

  2. issubclass 和 isinstance和断点调试

    issubclass 和 isinstance和断点调试 一.issubclass 判断第一个类是不是第二个类的子类,返回True或Flase class Foo: pass class Bar(Fo ...

  3. Two progressions CodeForces - 125D (暴力)

    大意: 给定序列, 求划分为两个非空等差序列. 暴搜, 加个记忆化剪枝. #include <iostream> #include <sstream> #include < ...

  4. 将数据库模型放入到.Net Core的类库中

    一.前提概要 今年某天突然无聊,就决定学习.net core,此时的版本已经是.net core 1.1了.之前一直是用.net framework做项目,一直对Html.EditFor()等Html ...

  5. jstl用法 简介

    <c:choose> <c:when test="${salary <= 0}"> 太惨了. </c:when> <c:when t ...

  6. Linux学习--第十天--bash脚本、用户自定义变量、环境变量、位置参数变量、预定义变量、标准输入输出、wc、history、dd、PS1

    shell简介 分为两种c shell 和b shell b shell:sh.ksh.Bash.psh.zsh: (Bash和sh兼容,linux基本shell是Bash) c shell:csh. ...

  7. 认识 android-job

    简评: Android 实现后台任务的最佳实践. 对于现在的应用来说,在应用生命周期之外运行一些后台任务可以说已经是一项必不可少的需求了.这些任务可能是在某个时间点提醒用户什么事情或同步本地数据到服务 ...

  8. Atcoder Regular 097 相邻球交换目的递增DP

    A /*Huyyt*/ #include<bits/stdc++.h> #define mem(a,b) memset(a,b,sizeof(a)) #define pb push_bac ...

  9. RSA前端加密

    昨天做了登陆模块,接触了md5&RSA加密.有点意思,talk is cheap,show me the code! 前端加密 为什么要加密 前端加密的方式 后台如何解密 1 为什么要加密? ...

  10. Ubuntu 16.04 编译ORB_SLAM2_modified

    编译g2o_with_orbslam2 1.修改g2o/types/slam2d/edge_se2_pointxy_bearing.cpp t.setRotation(t.rotation().ang ...